Opened 10 years ago
Last modified 10 years ago
#24447 closed Bug
Migrations do not add a FK constraint for an existing column — at Version 4
Reported by: | Jean-Louis Fuchs | Owned by: | nobody |
---|---|---|---|
Component: | Migrations | Version: | 1.7 |
Severity: | Normal | Keywords: | |
Cc: | Markus Holtermann | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | yes |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Pull request: https://github.com/django/django/pull/4235
Consider following model:
class ForeignKeyTest(models.Model): id = models.AutoField(primary_key=True) customer = models.IntegerField() class Customer(models.Model): id = models.AutoField(primary_key=True)
Then it gets migrated to this:
class ForeignKeyTest(models.Model): id = models.AutoField(primary_key=True) customer = models.ForeignKey('Customer') class Customer(models.Model): id = models.AutoField(primary_key=True)
The second migration won't create the foreign key. This does not fail with sqlite! Because these migrations are handled differently. It will fail with MySQL and probably Postgres too.
The code didn't detect this case: if old_field.rel doesn't exist alter_field() must always create the foreign key and ONLY if .rel exists it must check .db_constraint, too. Since no .rel also means there was no foreign key before.
Change History (4)
comment:1 by , 10 years ago
Patch needs improvement: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 10 years ago
Version: | master → 1.7 |
---|
comment:3 by , 10 years ago
Cc: | added |
---|---|
Needs documentation: | set |
comment:4 by , 10 years ago
Description: | modified (diff) |
---|---|
Summary: | Migrations do not execute create_fk_sql() when adding a foreign key to a field → Migrations do not add a FK constraint for an existing column |
Removed redundant information and chose clearer title.