There is a six years old issue which has a working patch:
https://code.djangoproject.com/ticket/10827If a migration deletes and recreates content-types, then the automatically called create_migrations()
can result in ForeignKey constraint errors.
Example:
If Permission "Can add group" had primary key 1 before the migration and after the migration
it has the value 2, then things get broken.
The cache needs to be cleared first.
Please have a look at it.
Thank you,
Thomas Güttler