clwainwright
clwainwright

Reputation: 1704

How to switch the direction of a Django OneToOneField?

I currently have a couple of models set up like this:

from django.db import models

class Child(models.Model):
    child_name = models.CharField(max_length=200)

class Parent(models.Model):
    parent_name = models.CharField(max_length=200)
    child = models.OneToOneField(Child, null=True, blank=True)

Unfortunately this is wrong, because I want a delete on the parent to cascade to the child and not vice-versa, so I really should have them set up like this:

class Parent(models.Model):
    parent_name = models.CharField(max_length=200)

class Child(models.Model):
    child_name = models.CharField(max_length=200)
    child = models.OneToOneField(Parent)

Assuming that I don't currently have any orphan children, how can I set up the django migration such that my data remains intact? I'm using a Postgres database, if that makes any difference.

Upvotes: 6

Views: 1814

Answers (1)

Alasdair
Alasdair

Reputation: 308879

First, add a OneToOneField to the child model, while keeping the original field.

class Parent(models.Model):
    parent_name = models.CharField(max_length=200)
    child = models.OneToOneField('Child', null=True, blank=True, related_name='+')

class Child(models.Model):
    child_name = models.CharField(max_length=200)
    parent = models.OneToOneField(Parent, null=True, blank=True, related_name='+')

I've set the related_name='+' so that the reverse relationships do not clash with the other field. You might find the data migration is easier if you specify a related name for one or both of the fields.

Create a migration for the new field.

Then create a data migration to populate the new field.

Finally, remove the old field, and create a new migration.

Upvotes: 10

Related Questions