Thursday, November 2, 2017

Remove/undo a multi table inheritance

Hi,

I have introduced a multi table inheritance in the past. Now I am trying to remove it again by adding an explicit one-to-one relation from the child table to the parent. So my starting point is:
 
from django.db import models

class Place(models.Model):
 name
= models.CharField(max_length=50)
 address
= models.CharField(max_length=80)

class Restaurant(Place):
 serves_hot_dogs
= models.BooleanField(default=False)
 serves_pizza
= models.BooleanField(default=False)

And I want to end up with something similar to this:

from django.db import models

class Place(models.Model):
 name
= models.CharField(max_length=50)
 address
= models.CharField(max_length=80)

class Restaurant(models.Model):
 place_ptr
= models.OneToOneField(Place)
 serves_hot_dogs
= models.BooleanField(default=False)
 serves_pizza
= models.BooleanField(default=False)

When running make migrations it will prompt me for an initial value for the newly introduced `id` field, which I don't know howto provide.

I attempt the following fixes:

   - introducing an Integer field, copy data from the implicit OneToOneField and making both explicit while switiching the primary_key at the same time (fails in sqlite with NOT NULL constraint and primary key issues on postgres)
   - writing a SplitDatabaseAndState migration where in the State part the model is deleted and recreated, while in the database part the shema editor is used to alter the fields as needed, that didn't work either

So there are two questions that come to my mind:

1. What is the proper way to undo a multi table inheritance?
2. Is there even an official way to change the primary key of a model in Django, that does work without SQL migrations?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ff8be877-1eb8-419b-b456-89d20e9c09d8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment