Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#32334 closed Bug (invalid)

Backwards migration with RenameModel om MySQL failing with content_type.name does not exist

Reported by: Gerben Morsink Owned by: nobody
Component: Database layer (models, ORM) Version: 2.2
Severity: Normal Keywords:
Cc: Adam Johnson Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (2)

comment:1 by Mariusz Felisiak, 4 years ago

Cc: Adam Johnson added
Resolution: invalid
Status: newclosed

Thanks for the ticket, however Django 2.2 doesn't receive bugfixes anymore. Also, Django doesn't use ContentType.name anymore, so this can be an issue with some incompatible 3rd party package.

Closing per TicketClosingReasons/UseSupportChannels.

comment:2 by Gerben Morsink, 4 years ago

I was using a custom migration like:

TARGET_APP = 'auth'


class Migration(migrations.Migration):

    def __init__(self, name, app_label):
        # overriding application operated upon
        super(Migration, self).__init__(name, TARGET_APP)

    replaces = (
        (TARGET_APP, '0001_initial'),)

    dependencies = [
        ('contenttypes', '0001_initial'),
    ]

To migrate the Permission model and include permission names for other languages
When I replaced this with:

TARGET_APP = 'auth'


class Migration(migrations.Migration):

    def __init__(self, name, app_label):
        # overriding application operated upon
        super(Migration, self).__init__(name, TARGET_APP)

    replaces = (
        (TARGET_APP, '0001_initial'),)

    dependencies = [
        ('contenttypes', '0002_remove_content_type_name'),
    ]

It worked again.

Last edited 4 years ago by Gerben Morsink (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top