﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
33586	Cannot delete object with a foreign key dependency if the object also has a foreign key to a custom User model	Jeremy Poulin	nobody	"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.
"	Bug	new	Migrations	4.0	Normal				Unreviewed	0	0	0	0	0	0
