Thursday, November 2, 2017

Re: Remove/undo a multi table inheritance

Does it work if you do it in several stages (each one is a separate
migration action):

* Add the OneToOneField, make it nullable, still using MTI
* Add a python migration that populates it from the existing MTI information
* Remove the MTI
* Make the 1-2-1 field as you like it (remove null=True etc)

Of course, this won't get rid of MTI, as MTI is simply a normalized
way of specifying that other models have a one to one correspondence
with a base model - if you express this as model inheritance, or if
you express it as an explicit 1-2-1 with a base model makes no
difference to the underlying structure of the tables, and hence any
performance characteristics will not change, so why bother?

Cheers

Tom

On Thu, Nov 2, 2017 at 9:11 AM, <yves.mueller@liqd.de> wrote:
> 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.

--
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/CAFHbX1L2O%2Bw%3DGzvG1Y8zsFWqFTsEaPQDFPHiHSjK_MJN4MhgJA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment