[Django]-Django tests failing after migrating to 1.2.5 – primary key issue for child model

4👍

This turned out to be a django bug. Details here: http://code.djangoproject.com/ticket/12728

And here is our temporary workaround: https://bitbucket.org/filmaster/filmaster-test/changeset/afbac905cf63

👤michuk

0👍

I can’t reproduce it on trunk or 1.2.3; I created a fresh project with app ‘threadedcomments’ and the models you posted with following result:

BEGIN;
TRUNCATE "threadedcomments_threadedcomment", "auth_permission", "auth_group", "auth_group_permissions", "django_session", "auth_user_groups", "auth_user_user_permissions", "threadedcomments_object", "auth_message", "django_site", "auth_user", "django_content_type";
SELECT setval(pg_get_serial_sequence('"auth_permission"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"auth_group"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"auth_user"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"auth_message"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"django_content_type"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"django_site"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"threadedcomments_object"','id'), 1, false);
COMMIT;

Sequence threadedcomments_threadedcomment_id_seq is never created and Django doesn’t try to flush it. I don’t think it’s a version change related bug (rather something that was always there, but went unnoticed).

👤lqc

0👍

You said, “ThreadedComment does not have a unique primary key of its own, relying on Object’s promary [sic] key (‘ID’).” Django requires one field to be identified as a primary key. See Automatic primary key fields.

More details . . .

The function pg_get_serial_sequence() returns the name of the sequence used by a specific column. So the following statement means, “Get the name of the sequence used for the ‘id’ column in the table ‘threadedcomments_threadedcomment’, set the sequence’s last_value field to 1, and return 1 the next time nextval() is called.

SELECT setval(pg_get_serial_sequence('"threadedcomments_threadedcomment"','id'), 1, false);

But this statement

SELECT setval('"threadedcomments_threadedcomment_id_seq"', 1, false);

means, “Set the last_value field of the sequence named ‘threadedcomments_threadedcomment_id_seq’ to 1, and return 1 the next time nextval() is called.”

My guess is that the algorithm Django uses to name sequences changed between versions. I’ve seen other systems make similar changes because identifiers built up by concatenating table and column names became too long. I think Oracle identifiers have a 30-character limit; your identifier is 39 characters. I’ve seen Rails generate 70+ character identifiers, which break on PostgreSQL (63 or 64 character limit).

Leave a comment