Opened 7 years ago

Closed 7 years ago

#27315 closed Uncategorized (needsinfo)

IntegrityError: insert or update on table violates foreign key constraint on Django 1.10

Reported by: Alex Rothberg Owned by: nobody
Component: Uncategorized Version: 1.10
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I am running my test suite on Django 1.10 (it passes on Django 1.09) and am seeing a number of failures like:

IntegrityError: insert or update on table "item_storage_books" violates foreign key constraint "item_storage_s_capture_user_id_a4181270_fk_users_myuser_user_id"
DETAIL:  Key (capture_user_id)=(alex) is not present in table "users_myuser".

On the Book model I have set db_constaint=False:

    capture_user = models.ForeignKey(
        settings.AUTH_USER_MODEL, editable=False, null=False, blank=False,
        db_constraint=False, to_field='user_id')

Change History (5)

comment:1 by Tim Graham, 7 years ago

Can you provide a sample project that reproduces the issue and can you bisect to find the commit where the failure was introduced? It might be fcd08c175787e909b3eb98f756317a07741c48dd.

comment:2 by Alex Rothberg, 7 years ago

I think the issue has something to do with the migration engine. I have one migration now with a series of operations. One of the earlier operations CreateModel (with the field set to db_constraint=True); a later operation (in the same migration) then sets db_constraint=False. This all works in Django 1.9 but not 1.10.

If I modify the earlier CreateModel operation to set the field initially to db_constraint=False, then my tests all pass.

comment:4 by Alex Rothberg, 7 years ago

This seems to be the commit that is causing the issues: https://github.com/django/django/commit/fcd08c175787e909b3eb98f756317a07741c48dd

comment:5 by Tim Graham, 7 years ago

Without a sample project to reproduce the issue, it's difficult for me to say if this is something that Django should/can fix or if it's an issue in your project that you'll need to adapt for that change.

comment:6 by Tim Graham, 7 years ago

Resolution: needsinfo
Status: newclosed

I tried to reproduce, but didn't have any luck, except if I removed the migration that changed the field to db_constraint=False. Please reopen if you can provide a sample project and hopefully explain why Django is at fault. Thanks!

Note: See TracTickets for help on using tickets.
Back to Top