#How to migrate an inherited model into being abstract?

1 messages ยท Page 1 of 1 (latest)

pastel frigate
#

We have a legacy codebase that has a model A and B that both inherit from non-abstract model C. The codebase is old enough that abstract models were not a thing yet in Django, is my understanding.

Needless to say, this is causing problems and we would like to migrate C into being an abstract model. However, there doesn't appear to be a clean way to do so. Complications also include the fact that C includes a 'parent' field that points to other C objects (this should become A having a parent field that points to other As, and B having a fk pointing to A objects), as well as other seperate models also having FK to A and B, which sems to be implemented as FKs towards C instead for some odd reason.

Right now I'm trying to create each of the C fields on A and B as 'temp_<fieldname>' fields, using RunPython to copy the data over into the temp fields, delete the C_ptr field, and then rename the fields to their proper names with RenameField, and finally delete the C model.

Right now it's complaining that it cannot delete C_ptr because there's things relying on it, but I don't see how I can change that, because all the models 'relying' on it, aren't even pointed at C at all- they're all pointed at A and B.

What's the proper way to migrate this without losing data?

pastel frigate
#

or, if nothing else, what's the proper way to change the FK on the various other models that point at A and B to actually point at A and B?

pastel frigate
#

to_field to point to temp_id won't work, since manytomanyfield and onetoone field has no equivalent.

#

is SeparateDatabaseAndState() the way to go? Feels a bit hacky...

pastel frigate
#

From a quick look, this looks very promising! I'll give it a try tommorow when I can

visual wasp
#

Good luck!

pastel frigate
#

thanks!

pastel frigate
#

out of curiosity, did you ever submit that as a ticket? I would +1 it

visual wasp
#

I don't believe I did. I'll do that today ๐Ÿ˜…

visual wasp
#

It doesn't need an upvote.

visual wasp
pastel frigate
#

oof lol

pastel frigate
#

Running into an issue. Admin refers to "parent" for those classes in several places, which breaks makemigrations and migrate

#

E.g. "The value of auto complete_fields refers to 'parent', which is not a field of B"

#

Parent was originally a FK to "self" on C

#

Making BaseC have that FK, or even A or B, doesnt seem to do anything?

#

Is --skip-checks thr right solution here? Because it doesnt feel like the right solution here

#

But also why is it even checkibg the admin settings during make migrations/migrate? It KNOWS its going to reference fields at import time that may or may not exist yet...?

visual wasp
#

Your application code needs to be compatible with the models at all times, including the admin.

pastel frigate
#

Or I guest alternately, why does it not think the parent field I added on A/B doesnt exist?

#

That's the thing, it does. A and B have a parent field right now

visual wasp
#

It feels unlikely that it's getting that wrong, so my guess is there's a small mistake somewhere in your code.

pastel frigate
#

Not sure what it could be. Moving parent into BaseC doesnt change anything. The AST parses

#

B and A shouod both have a parent field.

#

Moving the FK to A and B (which is the better method anyway, since its a FK to self, and BaseC doesnt have a model) also changes nothing.

#

Runserver even runs

#

It errors out because there's migrations that need made/applied if you load a page, but it starts up with no errors

#

So I dont think there's a syntax error or anything

pastel frigate
#

I think things are just way too interconnected.

#

I'm going to just try to start moving fields out of C into B and A directly by creating fieldname_temp on A/B, and then migrating the data into them, then deleting the field in C, and then renaming the fields in A/B

#

just not have a base

#

probably easier to remove the base, then add an abstract base later (at least for the functions, if not the fields)

visual wasp
#

I think that'd work