Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove app reference after model dependencies were added

I have a django app that was utilizing an external app reference. we no longer need this app reference, so we want to remove the dependency. I am able to generate the migrations to remove the foreign keys to models within the app we are removing, but I cannot remove the requirement reference in the app because it is referenced in the earlier migrations.

Does anyone have a known solution to removing app requirements like this?

to provide an example, say I have an external app that has the following model:

class ExternalInfo(models.Model):
    random_field = CharField()

and in your project application, you have a customer record:

class Customer(models.Model):
    name = CharField()
    external_info = ForegnKey(external_app.ExternalInfo, null=True)

migration 0001 of the application creates the foreign key to the model in the external app. 10 months go by, and 10 migrations later, you need to remove external_info from the customer model, and you want to remove the external_app from the project. How do I remove this dependency without breaking older migrations?

like image 392
Nathan Tregillus Avatar asked Jul 17 '26 15:07

Nathan Tregillus


1 Answers

Ok, so my workaround this issue was to do the following steps in order:

  1. generate the migration script that would remove the external_info column from the customer model
  2. removed all code that referenced said field
  3. modified the 0001 migration to create the foreign key field to a different model that existed prior to the 0001 migration
  4. removed the app from the pip requirements and installed apps

following these steps both upgrading existing databases, and building from scratch result in the same database schema. (excluding that I now need to find a way to get rid of the tables the app had created, anyone have ideas on best approach? I was thinking of writing a migrations.RunSql to drop the tables, but they won't exist in newly created databases)

like image 110
Nathan Tregillus Avatar answered Jul 23 '26 02:07

Nathan Tregillus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!