﻿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
32334	Backwards migration with RenameModel om MySQL failing with content_type.name does not exist	Gerben Morsink	nobody	"I have a relatively simple migration:

{{{
        migrations.RenameModel(
            old_name='OldModel',
            new_name='NewModel',
        ),
}}}
which is executed fine.

After which I do 
{{{python manage.py remove_stale_contenttypes}}}

When I then revert the migration, I'm unexpectantly receiving the following error:    


{{{
Unapplying someapp.0002_auto_20210107_2123...Traceback (most recent call last):
  File ""/home/lib/python3.8/site-packages/django/contrib/contenttypes/models.py"", line 19, in get_by_natural_key
    ct = self._cache[self.db][(app_label, model)]
KeyError: 'default'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ""/home/lib/python3.8/site-packages/django/db/backends/utils.py"", line 84, in _execute
    return self.cursor.execute(sql, params)
  File ""/home/lib/python3.8/site-packages/django/db/backends/mysql/base.py"", line 71, in execute
    return self.cursor.execute(query, args)
  File ""/home/lib/python3.8/site-packages/MySQLdb/cursors.py"", line 206, in execute
    res = self._query(query)
  File ""/home/lib/python3.8/site-packages/MySQLdb/cursors.py"", line 319, in _query
    db.query(q)
  File ""/home/lib/python3.8/site-packages/MySQLdb/connections.py"", line 259, in query
    _mysql.connection.query(self, query)
MySQLdb._exceptions.OperationalError: (1054, ""Unknown column 'django_content_type.name' in 'field list'"")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File ""manage.py"", line 10, in <module>
    execute_from_command_line(sys.argv)
  File ""/home/lib/python3.8/site-packages/django/core/management/__init__.py"", line 381, in execute_from_command_line
    utility.execute()
  File ""/home/lib/python3.8/site-packages/django/core/management/__init__.py"", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File ""/home/lib/python3.8/site-packages/django/core/management/base.py"", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File ""/home/lib/python3.8/site-packages/django/core/management/base.py"", line 364, in execute
    output = self.handle(*args, **options)
  File ""/home/lib/python3.8/site-packages/django/core/management/base.py"", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File ""/home/lib/python3.8/site-packages/django/core/management/commands/migrate.py"", line 232, in handle
    post_migrate_state = executor.migrate(
  File ""/home/lib/python3.8/site-packages/django/db/migrations/executor.py"", line 121, in migrate
    state = self._migrate_all_backwards(plan, full_plan, fake=fake)
  File ""/home/lib/python3.8/site-packages/django/db/migrations/executor.py"", line 196, in _migrate_all_backwards
    self.unapply_migration(states[migration], migration, fake=fake)
  File ""/home/lib/python3.8/site-packages/django/db/migrations/executor.py"", line 269, in unapply_migration
    state = migration.unapply(state, schema_editor)
  File ""/home/lib/python3.8/site-packages/django/db/migrations/migration.py"", line 172, in unapply
    operation.database_backwards(self.app_label, schema_editor, from_state, to_state)
  File ""/home/lib/python3.8/site-packages/django/db/migrations/operations/special.py"", line 196, in database_backwards
    self.reverse_code(from_state.apps, schema_editor)
  File ""/home/lib/python3.8/site-packages/django/contrib/contenttypes/management/__init__.py"", line 42, in rename_backward
    self._rename(apps, schema_editor, self.new_model, self.old_model)
  File ""/home/lib/python3.8/site-packages/django/contrib/contenttypes/management/__init__.py"", line 20, in _rename
    content_type = ContentType.objects.db_manager(db).get_by_natural_key(self.app_label, old_model)
  File ""/home/lib/python3.8/site-packages/django/contrib/contenttypes/models.py"", line 21, in get_by_natural_key
    ct = self.get(app_label=app_label, model=model)
  File ""/home/lib/python3.8/site-packages/django/db/models/manager.py"", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File ""/home/lib/python3.8/site-packages/django/db/models/query.py"", line 402, in get
    num = len(clone)
  File ""/home/lib/python3.8/site-packages/django/db/models/query.py"", line 256, in __len__
    self._fetch_all()
  File ""/home/lib/python3.8/site-packages/django/db/models/query.py"", line 1242, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File ""/home/lib/python3.8/site-packages/django/db/models/query.py"", line 55, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File ""/home/lib/python3.8/site-packages/django/db/models/sql/compiler.py"", line 1142, in execute_sql
    cursor.execute(sql, params)
  File ""/home/lib/python3.8/site-packages/django/db/backends/utils.py"", line 99, in execute
    return super().execute(sql, params)
  File ""/home/lib/python3.8/site-packages/django/db/backends/utils.py"", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File ""/home/lib/python3.8/site-packages/django/db/backends/utils.py"", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File ""/home/lib/python3.8/site-packages/django_mysql/apps.py"", line 52, in rewrite_hook
    return execute(sql, params, many, context)
  File ""/home/lib/python3.8/site-packages/django/db/backends/utils.py"", line 84, in _execute
    return self.cursor.execute(sql, params)
  File ""/home/lib/python3.8/site-packages/django/db/utils.py"", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File ""/home/lib/python3.8/site-packages/django/db/backends/utils.py"", line 84, in _execute
    return self.cursor.execute(sql, params)
  File ""/home/lib/python3.8/site-packages/django/db/backends/mysql/base.py"", line 71, in execute
    return self.cursor.execute(query, args)
  File ""/home/lib/python3.8/site-packages/MySQLdb/cursors.py"", line 206, in execute
    res = self._query(query)
  File ""/home/lib/python3.8/site-packages/MySQLdb/cursors.py"", line 319, in _query
    db.query(q)
  File ""/home/lib/python3.8/site-packages/MySQLdb/connections.py"", line 259, in query
    _mysql.connection.query(self, query)
django.db.utils.OperationalError: (1054, ""Unknown column 'django_content_type.name' in 'field list'"")
}}}

I find it very weird to receive this error on Django 2.2, since content_type.name is removed back in Django version 1.8."	Bug	closed	Database layer (models, ORM)	2.2	Normal	invalid		Adam Johnson	Unreviewed	0	0	0	0	0	0
