[Django] #26777: Cannot add foreign key constraint when migrating manytomany field

920 views
Skip to first unread message

Django

unread,
Jun 18, 2016, 10:25:08 AM6/18/16
to django-...@googlegroups.com
#26777: Cannot add foreign key constraint when migrating manytomany field
-------------------------------+--------------------
Reporter: Uninen | Owner: nobody
Type: Uncategorized | Status: new
Component: Migrations | Version: 1.9
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
I have a model that looks like


{{{
class LiveEvent(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4,
editable=False)

...

attendees = models.ManyToManyField(User, blank=True,
related_name="events_attended")

...

notify_subscriptions = models.ManyToManyField(User, blank=True,
related_name="events_subscribed")

}}}

when trying to add and migrate notify_subscriptions field I get an
`django.db.utils.IntegrityError: (1215, 'Cannot add foreign key
constraint')` error. When adding

{{{

'OPTIONS': {
"init_command": "SET foreign_key_checks = 0;",
},
}}}

to database settings , the error becames:
`django.db.utils.OperationalError: (1825, "Failed to add the foreign key
constraint on table 'levan_liveevent_notify_subscriptions'. Incorrect
options in FOREIGN KEY constraint
'slipmat/levan_liveevent_noti_liveevent_id_a4871abc_fk_levan_liveevent_id'")`

the migration file looks like:

{{{

operations = [
migrations.AddField(
model_name='liveevent',
name='notify_subscriptions',
field=models.ManyToManyField(blank=True,
related_name='events_subscribed', to=settings.AUTH_USER_MODEL),
),
]

}}}

I'm using MySQL version 5.6.27 and Django version 1.9.7

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

Django

unread,
Jun 18, 2016, 10:55:09 AM6/18/16
to django-...@googlegroups.com
#26777: Cannot add foreign key constraint when migrating manytomany field
----------------------------+--------------------------------------
Reporter: Uninen | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 1.9
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 charettes):

* status: new => closed
* needs_better_patch: => 0
* needs_tests: => 0
* type: Uncategorized => Bug
* needs_docs: => 0
* resolution: => needsinfo


Old description:

New description:

I have a model that looks like

{{{#!python


class LiveEvent(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4,
editable=False)
...
attendees = models.ManyToManyField(
User, blank=True, related_name="events_attended"
)
...
notify_subscriptions = models.ManyToManyField(
User, blank=True, related_name="events_subscribed"
)
}}}

when trying to add and migrate notify_subscriptions field I get the
following error:
{{{


django.db.utils.IntegrityError: (1215, 'Cannot add foreign key
constraint')`
}}}

When adding

{{{#!python


'OPTIONS': {
"init_command": "SET foreign_key_checks = 0;",
},
}}}

to database settings , the error became:

{{{


django.db.utils.OperationalError: (1825, "Failed to add the foreign key
constraint on table 'levan_liveevent_notify_subscriptions'. Incorrect
options in FOREIGN KEY constraint
'slipmat/levan_liveevent_noti_liveevent_id_a4871abc_fk_levan_liveevent_id'")
}}}

the migration file looks like:

{{{#!python


operations = [
migrations.AddField(
model_name='liveevent',
name='notify_subscriptions',
field=models.ManyToManyField(
blank=True, related_name='events_subscribed',
to=settings.AUTH_USER_MODEL
),
),
]
}}}

I'm using MySQL version 5.6.27 and Django version 1.9.7

--

Comment:

I just tried reproducing against Django 1.9.7 with no success.

Please re-open the ticket by answering these two questions:

1. Are both of your tables using the InnoDB engine? Foreign keys are not
supported on MyISAM.
2. Are you using a custom user model which you didn't provide the
definition?

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

Django

unread,
Jun 18, 2016, 11:20:51 AM6/18/16
to django-...@googlegroups.com
#26777: Cannot add foreign key constraint when migrating manytomany field
----------------------------+--------------------------------------
Reporter: Uninen | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.9
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 Uninen):

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


Comment:

1. Yes, both tables are using InnoDB engine.
2. No, I'm not using a custom user model

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

Django

unread,
Jun 18, 2016, 11:47:28 AM6/18/16
to django-...@googlegroups.com
#26777: Cannot add foreign key constraint when migrating manytomany field
----------------------------+--------------------------------------
Reporter: Uninen | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.9
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 Uninen):

FWIW, adding `db_constraint=False` to the `notify_subscriptions` field
produces a migration that runs flawlessly. That's my workaround for now.

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

Django

unread,
Jun 18, 2016, 2:18:43 PM6/18/16
to django-...@googlegroups.com
#26777: Cannot add foreign key constraint when migrating manytomany field
----------------------------+--------------------------------------
Reporter: Uninen | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.9
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 charettes):

Are you using Oracle's MySQL backend or `django.db.backends.mysql`?

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

Django

unread,
Jun 18, 2016, 9:49:40 PM6/18/16
to django-...@googlegroups.com
#26777: Cannot add foreign key constraint when migrating manytomany field
----------------------------+--------------------------------------
Reporter: Uninen | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 1.9
Severity: Normal | Resolution: invalid
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 charettes):

* cc: charettes (added)


* status: new => closed

* resolution: => invalid


Comment:

Please inspect your database and provide us with the datatype of both
`LiveEvent`'s table `id` column and the intermediary table's
(`notify_subscriptions`) column referencing the former.

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

Reply all
Reply to author
Forward
0 new messages