Stale content type prompt deleting all model instances after renaming django model with permissions

895 views Asked by At

I had two models called CombinedProduct and CombinedProductPrice which I renamed to Set and SetPrice respectively. I did this by changing their model name in the models.py file and replaced all occurrences of it. This also included renaming a foreignkey field in another model from combined_product to set (pointing to a CombinedProduct).

When running makemigrations django properly detected the renaming and asked if I had renamed all three of those things and I pressed 'yes' for all. However when running 'migrate', after applying some stuff, I get asked:

The following content types are stale and need to be deleted:

    product | combinedproduct
    product | combinedproductprice

Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?
If you're unsure, answer 'no'.

I backed up my data and entered 'yes' which deleted all instances of Set (previously CombinedProduct) and SetPrice (previously CombinedProductPrice). If I roll back and tick no, then this question comes up every time I migrate.

This is weird since I don't use any of the django ContentType framework anywhere. When inspecting which fields point to ContentType however I see that auth.permission points to it, and I use permissions for those models. So maybe the deletion cascades from old permissions pointing to the old model names which in turn would delete my instances? If that is the case, how can I prevent this situation?

This is the migration that was generated:

 operations = [ 
    migrations.RenameModel(
        old_name='CombinedProduct',
        new_name='Set',
    ),  
    migrations.RenameModel(
        old_name='CombinedProductPrice',
        new_name='SetPrice',
    ),  
    migrations.AlterModelOptions(
        name='setprice',
        options={'ordering': ('set', 'vendor', 'price'), 'verbose_name': 'Set price', 'verbose_name_plural': 'Set prices'},
    ),  
    migrations.RenameField(
        model_name='setprice',
        old_name='combined_product',
        new_name='set',
    ),  
] 
1

There are 1 answers

1
Edwin Lunando On

If you want to rename your table, please take a look to RenameModel. Yes, Django do not detect the renamed model. So, you need to add it manually.