Opened 9 years ago
Closed 9 years ago
#26777 closed Bug (invalid)
Cannot add foreign key constraint when migrating manytomany field
Reported by: | Ville Säävuori | Owned by: | nobody |
---|---|---|---|
Component: | Migrations | Version: | 1.9 |
Severity: | Normal | Keywords: | |
Cc: | Simon Charette | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
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 the following error:
django.db.utils.IntegrityError: (1215, 'Cannot add foreign key constraint')`
When adding
'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:
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
Change History (5)
comment:1 by , 9 years ago
Description: | modified (diff) |
---|---|
Resolution: | → needsinfo |
Status: | new → closed |
Type: | Uncategorized → Bug |
comment:2 by , 9 years ago
Resolution: | needsinfo |
---|---|
Status: | closed → new |
- Yes, both tables are using InnoDB engine.
- No, I'm not using a custom user model
comment:3 by , 9 years ago
FWIW, adding db_constraint=False
to the notify_subscriptions
field produces a migration that runs flawlessly. That's my workaround for now.
comment:5 by , 9 years ago
Cc: | added |
---|---|
Resolution: | → invalid |
Status: | new → closed |
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.
I just tried reproducing against Django 1.9.7 with no success.
Please re-open the ticket by answering these two questions: