﻿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
32787	ContentTypes are created instead of renamed when using SeparateDatabaseAndState	David Seddon	nobody	"When renaming a model using `SeparateDatabaseAndState`, rather than renaming the existing `ContentType`, a new one is created. This breaks any existing generic foreign keys pointing to the model.

**Cause**

When a model is renamed in the standard way, the content types framework uses the pre_migrate signal to rename the `ContentType` model, ensuring that existing generic foreign keys continue to work. Code here: https://github.com/django/django/blob/main/django/contrib/contenttypes/management/__init__.py#L79

If `SeparateDatabaseAndState` is used to control the migration, the `isinstance` check will miss the fact that there is a `RenameModel` operation, as it just checks the containing `SeparateDatabaseAndState` operation. It therefore fails to inject a `RenameContentType` operation.

Later in the migration process, in [https://github.com/django/django/blob/main/django/contrib/contenttypes/management/__init__.py#L105 create_contenttypes] will be triggered by the `post_migrate` signal. Since there is now a missing ContentType for the new model name, it will create it.

**Discussion**

While `SeparateDatabaseAndState` is of course meant to be a low level operation, it's difficult to anticipate this behaviour. Since the ContentTypes table is being changed by in any event, it would seem to me preferable to do it in a backward-compatible way.

Alternatively, emitting a warning might be worthwhile: the developer could then manually add a `RenameContentType` to the migration file once they're aware of the issue.

I think this could be solved by adjusting [https://github.com/django/django/blob/main/django/contrib/contenttypes/management/__init__.py#L105 the check] so that it drills down to the `database_operations` if the operation is a `SeparateDatabaseAndState` instance."	New feature	closed	Database layer (models, ORM)	3.2	Normal	wontfix			Unreviewed	0	0	0	0	0	0
