Reputation: 17859
So I have the given model:
class FooBar(models.Model):
foo = models.ForeignKey(Foo,null=True,blank=True)
bar = models.ForeignKey(Bar,null=True,blank=True)
foo_flag = models.BooleanField(default=False)
bar_flag = models.BooleanField(default=False)
where the logic was that at all times, there can either be a foreign key to Foo
or Bar
and not both.But now the logic has changed so that there is always a Foo
foreign key and sometimes a Bar
. So my new model looks as such:
class FooBar(models.Model):
foo = models.ForeignKey(Foo)
bar = models.ForeignKey(Bar,null=True,blank=True)
bar_flag = models.BooleanField(default=False)
Now here is the complex part. The Bar
model looks as such:
class Bar(models.Model):
foo = models.ForeignKey(Foo)
so for every previously existing item in the database where the foo
field is null
and therefore there is a foreign key to Bar
, I need the foo
field to get a foreign key to the same Foo
object that has the bar
fields object has a foreign key to. Here is the logic stepped out:
FooBar.foo_flag
foo
foreign keys with the Foo
objects from the
Bar
foreign keysfoo
fieldHow could I go about writing this migration?
Upvotes: 0
Views: 1592
Reputation: 6787
Best practices for situations like that is make 3 independent migrations:
After migration, I recommend check tables for fake Foo object links and fix data manually if something go wrong.
Upvotes: 2