evildmp / Arkestra

Arkestra extends Django CMS to provide an intelligent semantic web publishing system for organisations and institutions.
http://arkestra-project.org
BSD 2-Clause "Simplified" License
139 stars 27 forks source link

Reopening issue #39: IntegrityError while importing example database (postgres) #122

Open fp4code opened 10 years ago

fp4code commented 10 years ago

I reopen the issue #39. Loading example_database.json seems not possible if using postgres.

1) Postgresql, with no database: $ dropdb base_django01 $ python manage.py syncdb --noinput --all ... psycopg2.OperationalError: FATAL: database "base_django01" does not exist

2) Postgresql, with fresh database: $ createdb -T template0 base_django01 $ python manage.py syncdb --noinput --all $ python manage.py loaddata example_database.json .../python2.7/site-packages/cms/utils/plugins.py:125: DuplicatePlaceholderWarning: Duplicate placeholder found: body warnings.warn("Duplicate placeholder found: %s" % placeholder, DuplicatePlaceholderWarning) ... IntegrityError: Could not load cms.Page(pk=1): duplicate key value violates unique constraint "cms_placeholder_pkey" DETAIL: Key (id)=(1) already exists.

trying a second loaddata, I get DETAIL: Key (id)=(2) already exists.

3) Sqlite $ python manage.py loaddata example_database.json .../python2.7/site-packages/cms/utils/plugins.py:125: DuplicatePlaceholderWarning: Duplicate placeholder found: body warnings.warn("Duplicate placeholder found: %s" % placeholder, DuplicatePlaceholderWarning) Installed 1039 object(s) from 1 fixture(s)

evildmp commented 10 years ago

I will have a look and make sure the JSON database file is up-to-date.

evildmp commented 10 years ago

Can you try again, following the instructions at http://arkestra.readthedocs.org/en/latest/getting_started/installation.html (which have been updated) but using the file https://www.dropbox.com/s/ya81gft1jf8wxm7/example_database.json instead of the one in the repository?

fp4code commented 10 years ago

Same problem.

In addition to the instructions, I have installed psycopg2:

pip install psycopg2

and added these lines to manage.py:

To get all sql queries sent by Django from py shell

import logging l = logging.getLogger('django.db.backends') l.setLevel(logging.DEBUG) l.addHandler(logging.StreamHandler())

The last lines on stdout are:

.../python2.7/site-packages/cms/utils/plugins.py:125: DuplicatePlaceholderWarning: Duplicate placeholder found: body warnings.warn("Duplicate placeholder found: %s" % placeholder, DuplicatePlaceholderWarning) (0.002) SELECT "cms_placeholder"."id", "cms_placeholder"."slot", "cms_placeholder"."default_width" FROM "cms_placeholder" INNER JOIN "cms_page_placeholders" ON ("cms_placeholder"."id" = "cms_page_placeholders"."placeholder_id") WHERE "cms_page_placeholders"."page_id" = 1 ; args=(1,) (0.001) INSERT INTO "cms_placeholder" ("slot", "default_width") VALUES ('body', NULL) RETURNING "cms_placeholder"."id"; args=(u'body', None) Problem installing fixture 'example_database.json': Traceback (most recent call last): ... IntegrityError: Could not load cms.Page(pk=1): duplicate key value violates unique constraint "cms_placeholder_pkey" DETAIL: Key (id)=(1) already exists.

fp4code commented 10 years ago

The problem is clearly decribed here: http://jesiah.net/post/23173834683/postgresql-primary-key-syncing-issues

First, 367 rows of "cms_placeholder" table are filled giving the "id" column: INSERT INTO "cms_placeholder" ("id", "slot", "default_width") VALUES (1, 'body', NULL); ... INSERT INTO "cms_placeholder" ("id", "slot", "default_width") VALUES (367, 'body', NULL);

The problem comes when a row is filled with autoindexing: INSERT INTO "cms_placeholder" ("slot", "default_width") VALUES ('body', NULL) RETURNING "cms_placeholder"."id";

Before doing that, Django should resync the index from 1 to 368 calling something likel: "SELECT setval('cms_placeholder_id_seq', (SELECT MAX(id) FROM cms_placeholder)+1);"

fp4code commented 10 years ago

A certainly dirty workaround is to patch the file cms/models/pagemodel.py adding at the begining of the method Page.rescan_placeholders() the lines:

    from django.db import connection
    cursor = connection.cursor()
    cursor.execute("SELECT setval('cms_placeholder_id_seq', (SELECT MAX(id) FROM cms_placeholder)+1)")

That's seems a django-cms issue. I have filled #3193 https://github.com/divio/django-cms/issues/3193

evildmp commented 10 years ago

Thanks for the report. I was going to say to check that it still occurs using the latest version of django CMS, but I see you've done that now. In any case I will add a note to the documentation.

fp4code commented 10 years ago

The django-cms bug was fixed indeed in version 2.3.8 (2.3.7 still contains it).