#30591 closed Bug (fixed)
MySQL: 1215 - "Cannot add foreign key constraint" when altering type of unique field referenced by ForeignKey.
Reported by: | Cornelis Poppema | Owned by: | Adnan Umer |
---|---|---|---|
Component: | Migrations | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I have two models and a ForeignKey. In this project I'm still using django 1.11 (still busy migrating to python3) but I have reproduced this in django 2.2.2
Models are Call and Client. Call has a ForeignKey to a PositiveIntegerField on Client. I'm changing the type of this field to a CharField, but django/mysql cannot drop or add constraints.
I found this issue: https://code.djangoproject.com/ticket/28305. I had hoped I was still using a version of 1.11 that didn't include the backport but unfortunately for me I did.
Before creating this ticket I also came across https://code.djangoproject.com/ticket/30152 which is also related to my initial findings. As https://code.djangoproject.com/ticket/30152#comment:10 says, the internals are checking for relations that might have constraints, but in 30152's and my case it doesn't find the relation. As a result the foreign key constraint isn't dropped resulting in the error:
django.db.utils.OperationalError: (1833, "Cannot change column 'code': used in a foreign key constraint 'myapp_call_clientcode_907d4acf_fk_myapp_client_code' of table 'test_test.myapp_call'")
After either applying the patch https://code.djangoproject.com/attachment/ticket/30152/testcase_and_rough_fix.patch or simply (temporarily) removing related_name='+' I get past this OperationError. Instead I am confronted with:
django.db.utils.IntegrityError: (1215, 'Cannot add foreign key constraint')
From what I've read about MySQL constraints this is caused by the different field types. Changing the type of the original field doesn't change the type of the foreign key field and the constraint cannot be re-added after dropping it.
I created a project on github: https://github.com/cpoppema/django-migrate-constraint-bug, ignore the master-branch for now.
Branch with original issue: https://github.com/cpoppema/django-migrate-constraint-bug/tree/cannot-change-column-used-in-a-foreign-key-constraint
Branch with issue without related_name='+': https://github.com/cpoppema/django-migrate-constraint-bug/tree/cannot-add-foreign-key-constraint
Known workaround that I've come up with is:
run a migration to change ForeignKey on Call to PositiveIntegerField
run a migration to change the PositiveIntegerField on Client to CharField
run a migration to change the PositiveIntegerField on Call to ForeignKey
With this, both fields end up as a CharField. I'd rather have Django generate either multiple migrations or do an ALTER TABLE for related fields.
Attachments (1)
Change History (12)
comment:1 by , 5 years ago
Summary: | bug in manage.py migrate when changing the field type of a foreign key with a constraint → MySQL: 1215 - "Cannot add foreign key constraint" when altering type of unique field referenced by ForeignKey. |
---|---|
Triage Stage: | Unreviewed → Accepted |
Version: | 2.2 → master |
by , 5 years ago
Attachment: | ticket-30591.diff added |
---|
comment:2 by , 5 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:4 by , 5 years ago
Patch needs improvement: | set |
---|
comment:6 by , 5 years ago
Patch needs improvement: | unset |
---|
Thanks for detailed report. #30152 is related but it doesn't fix the main issue. I attached tests for both scenarios.
Reproduced at 8454f6dea49dddb821bfcb7569ea222bb487dfd1.