#33586: Cannot delete object (A) referenced by another object (B) if said object
(A) has a foreign key to a custom User model
-------------------------------+--------------------------------------
Reporter: Jeremy Poulin | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 4.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by Jeremy Poulin:
Old description:
> I've created a reproducer app:
> https://github.com/jaypoulz/rmigration_bug
>
> After installing dependencies, run the repro.sh script:
> https://github.com/jaypoulz/rmigration_bug/blob/main/repro.sh
>
> All this script does is clear the db, run the initial migrations (which
> will fail because no user exists yet), create a dummy super user, re-run
> the migration so that it completes, and then run a reverse migration.
>
> Object A has 2 ForeignKeys - the customer User and object B.
> Object B is not important, but must exist in order to trigger the
> mismatch in 'Fake' model comparison during the delete call.
>
> https://github.com/jaypoulz/rmigration_bug/blob/main/rmigrate/migrations/0002_create_sample_objects.py#L15
> {{{
> ValueError: Cannot query "A object (1)": Must be "A" instance.
> }}}
>
> This appears to be because delete first checks if any B instances need to
> be deleted (since it's a CASCADE on delete relationship).
> 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.
>
> For some reason, this only occurs when you have a custom User model.
New description:
I've created a reproducer app:
https://github.com/jaypoulz/rmigration_bug
After installing dependencies, run the repro.sh script:
https://github.com/jaypoulz/rmigration_bug/blob/main/repro.sh
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.
Object A has a single foreign key to a custom User object.
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.
https://github.com/jaypoulz/rmigration_bug/blob/main/rmigrate/migrations/0002_create_sample_objects.py#L15
{{{
ValueError: Cannot query "A object (1)": Must be "A" instance.
}}}
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).
For some reason, these identifiers do not match.
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.
For some reason, this only occurs when you have a custom User model.
If you comment out the User model, the issue no longer occurs.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/33586#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107017f9ffbf272-9c3e9c87-0053-40be-983e-5579426c0363-000000%40eu-central-1.amazonses.com.