0

We use polymorphic models with django-polymorphic.

Recently we changed the hierarchy of these models, from this:

class A(PolymorphicModel):
    # foo
class B(A):
    # bar
class C(A):
    # baz

to this:

class A(PolymorphicModel):
    # foo
class B(A):
    # bar
class C(B):  # C became a subclass of B.
    # baz

All fields in B have a default value, so in theory, there is a smooth way to migrate the existing data to the new schema. It needs to create rows in table B, and re-appropriate the baseclass column of table C.

However, South demands a one-off value for C.b_id (and also one for C.a_id, for the backwards migration).

I realize South is not aware of django-polymorphic. Also, django-polymorphic comes in the way when I want to "just add a row in B" in a datamigration.

What would be the most elegant hack to migrate the data in such a case?

1 Answer 1

1

The best solution I found was this:

  1. Schema migration: add model C_temp that has the same fields as C, but is derived from B.
  2. Data migration: copy all data from C to C_temp.

    For each object in C, create a new C_temp by copying all fields, including id. Supply default values for the fields in B.

    This will add the new row in B and connect it with the existing rows in A.

  3. Schema migration: delete C.

  4. Schema migration: rename C_temp to C.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.