[Django] #27315: IntegrityError: insert or update on table violates foreign key constraint on Django 1.10

221 views
Skip to first unread message

Django

unread,
Oct 5, 2016, 11:19:40 AM10/5/16
to django-...@googlegroups.com
#27315: IntegrityError: insert or update on table violates foreign key constraint
on Django 1.10
-------------------------------+--------------------
Reporter: Alex Rothberg | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.10
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
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')

}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27315>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 5, 2016, 12:06:01 PM10/5/16
to django-...@googlegroups.com
#27315: IntegrityError: insert or update on table violates foreign key constraint
on Django 1.10
-------------------------------+--------------------------------------

Reporter: Alex Rothberg | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Tim Graham):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

Can you provide a sample project that reproduces the issue and can you
[https://docs.djangoproject.com/en/dev/internals/contributing/triaging-
tickets/#bisecting-a-regression bisect] to find the commit where the
failure was introduced? It might be
fcd08c175787e909b3eb98f756317a07741c48dd.

--
Ticket URL: <https://code.djangoproject.com/ticket/27315#comment:1>

Django

unread,
Oct 5, 2016, 3:31:10 PM10/5/16
to django-...@googlegroups.com
#27315: IntegrityError: insert or update on table violates foreign key constraint
on Django 1.10
-------------------------------+--------------------------------------

Reporter: Alex Rothberg | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by Alex Rothberg):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/27315#comment:2>

Django

unread,
Oct 5, 2016, 3:45:51 PM10/5/16
to django-...@googlegroups.com
#27315: IntegrityError: insert or update on table violates foreign key constraint
on Django 1.10
-------------------------------+--------------------------------------

Reporter: Alex Rothberg | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by Alex Rothberg):

Sometime before: 354acd04af524ad82002b903df1189581c51cabe

--
Ticket URL: <https://code.djangoproject.com/ticket/27315#comment:3>

Django

unread,
Oct 5, 2016, 4:19:58 PM10/5/16
to django-...@googlegroups.com
#27315: IntegrityError: insert or update on table violates foreign key constraint
on Django 1.10
-------------------------------+--------------------------------------

Reporter: Alex Rothberg | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by Alex Rothberg):

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

--
Ticket URL: <https://code.djangoproject.com/ticket/27315#comment:4>

Django

unread,
Oct 5, 2016, 4:44:27 PM10/5/16
to django-...@googlegroups.com
#27315: IntegrityError: insert or update on table violates foreign key constraint
on Django 1.10
-------------------------------+--------------------------------------

Reporter: Alex Rothberg | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by Tim Graham):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/27315#comment:5>

Django

unread,
Oct 6, 2016, 5:09:17 PM10/6/16
to django-...@googlegroups.com
#27315: IntegrityError: insert or update on table violates foreign key constraint
on Django 1.10
-------------------------------+--------------------------------------

Reporter: Alex Rothberg | Owner: nobody
Type: Uncategorized | Status: closed
Component: Uncategorized | Version: 1.10
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => needsinfo


Comment:

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!

--
Ticket URL: <https://code.djangoproject.com/ticket/27315#comment:6>

Reply all
Reply to author
Forward
0 new messages