Django migrations relations

Viewed 55

Suppose we have two models, Person and Worker.

Know, I want to alter the name of Person to InterestPerson.

How does Django exactly knows that the new name belongs to the old one?

I was looking in the generated tables in DB and in the migrations, but I can't figure out how is he able to make the relation.

Same applies with fields, datatypes, lengths... etc.

1 Answers

If you rename Person to InterestPerson, you have technically removed the model called Person and added a new one called InterestPerson. When Django creates the migration files iterates through the new models and compares them to the deleted models, if a new model is similar (same definition) to a deleted model in the same app, Django does a rename operation with the database. Django isn't always sure that something has been renamed, in fact it sometimes prints questions in the console to figure out what/if anything has been renamed. Take a look at the definition of MigrationAutodetector (especially of the generate_renamed_models function), which is the code that detects renames.

Related