﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
23071	Can't create migration for apps that have ForeignKeys to each other	Jeroen Dekkers	nobody	"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."	Bug	closed	Migrations	1.7-rc-1	Release blocker	fixed			Accepted	1	0	0	0	0	0
