Migrations do not add a FK constraint for an existing column
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
(8)
Patch needs improvement: |
set
|
Triage Stage: |
Unreviewed → Accepted
|
Cc: |
Markus Holtermann added
|
Needs documentation: |
set
|
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
|
Triage Stage: |
Accepted → Ready for checkin
|
Resolution: |
→ fixed
|
Status: |
new → closed
|
Removed redundant information and chose clearer title.