Deleting a model cause error for old migrations that had referenced it

Hi

Suppose I have two apps named “core” and “accounting”. And back then, I had a model in the core app named cluster. In the accounting app, I had multiple models that related to this cluster model as a foreign key.
After a while, I deleted the cluster model and everything related to it. I migrated and didn’t have any problems.
But for a new database that I ran migrate, It started migrating from the first migration files and caused this error in those models in app b which had been related to cluster:

“ValueError: Related model ‘core.cluster’ cannot be resolved”

It happens because it runs all migrations in the core app, including the migration that deletes the cluster model. And after that, It runs migrations in the accounting app. In the third migration, which models reference to core.cluster, this error happens.

Could you help me? What did I do wrong?

And I have two general questions:

  1. Is it good to change old migrations manually? Like deleting these fk relations to cluster in accounting models in the old migrations.
  2. What is the best way to delete a model in a way that doesn’t lead to problems in other models and their old and new migration files?

Thank you.

Okay! If you are deleting any model or have made changes to the model of an app ,then doing migrations may sometime cause problem in your database.
First after deleting the model, also delete content from migrations folder (.py files starting from name 0001_initial.py till last) and also delete the same .py files from the pycache folder of migrations.
Then after this again run the migration command in terminal, You will find no issue with migrations.