#23071 closed Bug (fixed)
Can't create migration for apps that have ForeignKeys to each other
Reported by: | Jeroen Dekkers | Owned by: | nobody |
---|---|---|---|
Component: | Migrations | Version: | 1.7-rc-1 |
Severity: | Release blocker | 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
ForeignKey's to other apps create a dependency on __latest__
migration of that app. Because of this we can't create migrations where we have ForeignKey relations that go both ways, because that would result in app1 depending on __latest__
of app2 and app2 depending on __latest__
of app1. Way to reproduce:
Create the following model in myapp1:
class Model1(models.Model): field = models.CharField(max_length=10)
And the following model in myapp2:
class Model2(models.Model): field = models.CharField(max_length=10)
We run makemigations creating both initial migrations. We then add the following model in myapp1:
class Model3(models.Model): model2 = models.ForeignKey('myapp2.Model2')
We run makemigrations again, this will create a myapp1 migration that depends on __latest__
of myapp2.
Then we add the following model to myapp2:
class Model4(models.Model): model1 = models.ForeignKey('myapp1.Model1')
We then run makemigrations again, this will create a myapp2 migration that depends on __latest__
of myapp1. Running migrate then gives us a circular dependency error:
django.db.migrations.graph.CircularDependencyError: [('myapp1', u'0002_model3'), ('myapp2', u'0002_model4'), ('myapp1', u'0002_model3')]
I think the correct way to create dependency would be to create an explicit dependency on the latest migration of the other app at the time of creating the new ForeigKey instead of using __latest__
. This will mean that the second myapp1 migration will depend on myapp2's 0001_initial, and the second myapp2 migration will depend on myapp1's 0002_model3.
I tested this and it seems to work fine.
Change History (5)
comment:1 by , 10 years ago
Has patch: | set |
---|
comment:2 by , 10 years ago
Triage Stage: | Unreviewed → Accepted |
---|
Yeah, latest also doesn't work if at some point the target of the FK is deleted :/
comment:3 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
https://github.com/django/django/pull/2938