Changes between Initial Version and Version 1 of Ticket #33586
- Timestamp:
- Mar 18, 2022, 9:22:43 PM (3 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #33586
- Property Summary Cannot delete object with a foreign key dependency if the object also has a foreign key to a custom User model → Cannot delete object (A) referenced by another object (B) if said object (A) has a foreign key to a custom User model
-
Ticket #33586 – Description
initial v1 5 5 https://github.com/jaypoulz/rmigration_bug/blob/main/repro.sh 6 6 7 All this script does is clear the db, run the initial migrations (which will fail because no user exists yet), create a dummy superuser, re-run the migration so that it completes, and then run a reverse migration.7 All this script does is clear the DB, run the initial migrations (which will fail because no user exists yet), create a dummy superuser, re-run the migration so that it completes, and then run a reverse migration. 8 8 9 Object A has 2 ForeignKeys - the customer User and object B.10 Object B is not important, but must exist in order to trigger themismatch in 'Fake' model comparison during the delete call.9 Object A has a single foreign key to a custom User object. 10 Object B has a foreign key (many to one) relationships to object A. This object never needs to be instantiated, and in fact, only exists to trigger a mismatch in 'Fake' model comparison during the delete call. 11 11 12 12 https://github.com/jaypoulz/rmigration_bug/blob/main/rmigrate/migrations/0002_create_sample_objects.py#L15 … … 15 15 }}} 16 16 17 This appears to be because delete first checks if any B instances need to be deleted (since it's a CASCADE on delete relationship). 18 Even though there are no instances of B, A cannot be deleted because the model type returned by A via model lookup is not the same as the model referenced by B. 17 This appears to be because delete first checks if object A is referenced by any models that have a CASCADE on delete relationship, and discovers object B. It then compares the model identifier of object B's reference to object A with the instance type of the object that is going to be deleted (also object A). 18 19 For some reason, these identifiers do not match. 20 21 In other words, Even though there are no instances of B, A cannot be deleted because the model type returned by A via model lookup is not the same as the model type referenced by B. 19 22 20 23 For some reason, this only occurs when you have a custom User model. 24 25 If you comment out the User model, the issue no longer occurs.