# Buggy code
def resolve_auto_format(apps, schema_editor):
    Addon = apps.get_model("addons", "Addon")
    for addon in Addon.objects.filter(name="weblate.discovery.discovery"):
        if addon.configuration["file_format"] == "auto":
            detect = detect_filename(addon.configuration["match"].replace("\\.", "."))
            if detect is None:
                raise Exception(
                    "Existing component discovery with auto format, can not detect "
                    "file format. Please edit the format manually and rerun "
                    "migration. Affected component: {}/{}".format(
                        addon.component.project.slug, addon.component.slug
                    )
                )

            addon.configuration["file_format"] = detect.format_id
            addon.save()


# Traceback (MySQLdb._exceptions.OperationalError)
$ ./manage.py migrate --database=postgresql
Operations to perform:
Apply all migrations: accounts, addons, admin, auth, authtoken, checks, conten
ttypes, fonts, gitexport, lang, memory, screenshots, sessions, sites, social_dja
ngo, trans, weblate_auth, wladmin
Running migrations:
Applying weblate_auth.0001_initial... OK
Applying lang.0001_squashed_0011_auto_20180215_1158... OK
Applying trans.0001_squashed_0143_auto_20180609_1655... OK
Applying trans.0002_delete_indexupdate... OK
Applying trans.0003_remove_translation_commit_message... OK
Applying trans.0004_project_use_shared_tm... OK
Applying trans.0005_auto_20180914_1135... OK
Applying trans.0006_suggestion_userdetails... OK
Applying trans.0007_auto_20181011_1634... OK
Applying trans.0008_auto_20181015_2104... OK
Applying trans.0009_auto_20181113_1219... OK
Applying trans.0010_auto_20181205_1505... OK
Applying trans.0011_component_merge_message... OK
Applying trans.0012_auto_20190129_1325... OK
Applying trans.0013_component_addon_message... OK
Applying trans.0014_auto_20190203_1923... OK
Applying trans.0015_linked_component_branch... OK
Applying trans.0016_fix_alert_occurrence... OK
Applying trans.0017_component_language_code_style... OK
Applying trans.0018_remove_unusednewbase_alert... OK
Applying trans.0019_auto_20190321_1003... OK
Applying trans.0020_auto_20190321_0921... OK
Applying trans.0021_auto_20190321_1004... OK
Applying trans.0022_auto_20190321_1443... OK
Applying trans.0023_auto_20190325_1037... OK
Applying trans.0024_resolve_auto_format... OK
Applying social_django.0001_initial... OK
Applying accounts.0001_squashed_0037_auto_20180416_1406... OK
Applying accounts.0002_profile_uploaded... OK
Applying accounts.0003_profile_translate_mode... OK
ERROR Handled exception: OperationalError: (1054, "Unknown column 'accounts_profile.subscribe_any_translation' in 'field list'")
Applying accounts.0004_create_profile...Traceback (most recent call last):
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 71, in execute
return self.cursor.execute(query, args)
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/MySQLdb/cursors.py", line 206, in execute
res = self._query(query)
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/MySQLdb/cursors.py", line 312, in _query
db.query(q)
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/MySQLdb/connections.py", line 224, in query
_mysql.connection.query(self, query)
MySQLdb._exceptions.OperationalError: (1054, "Unknown column 'accounts_profile.subscribe_any_translation' in 'field list'")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "./manage.py", line 33, in <module>
main()
File "/home/weblate/weblate/weblate/runner.py", line 34, in main
execute_from_command_line(argv)
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 234, in handle
fake_initial=fake_initial,
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/db/migrations/operations/special.py", line 190, in database_forwards
self.code(from_state.apps, schema_editor)
File "/home/weblate/weblate/weblate/accounts/migrations/0004_create_profile.py", line 12, in create_profiles
Profile.objects.get_or_create(user=user)
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/db/models/query.py", line 538, in get_or_create
return self.get(**kwargs), False
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/db/models/query.py", line 402, in get
num = len(clone)
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/db/models/query.py", line 256, in __len__
self._fetch_all()
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/db/models/query.py", line 1242, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/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/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1100, in execute_sql
cursor.execute(sql, params)
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/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/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 71, in execute
return self.cursor.execute(query, args)
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/MySQLdb/cursors.py", line 206, in execute
res = self._query(query)
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/MySQLdb/cursors.py", line 312, in _query
db.query(q)
File "/home/weblate/.pyenv/versions/3.7.3/lib/python3.7/site-packages/MySQLdb/connections.py", line 224, in query
_mysql.connection.query(self, query)
django.db.utils.OperationalError: (1054, "Unknown column 'accounts_profile.subscribe_any_translation' in 'field list'")

# Fixed code
def resolve_auto_format(apps, schema_editor):
    Addon = apps.get_model("addons", "Addon")
    db_alias = schema_editor.connection.alias
    for addon in Addon.objects.using(db_alias).filter(
        name="weblate.discovery.discovery"
    ):
        if addon.configuration["file_format"] == "auto":
            detect = detect_filename(addon.configuration["match"].replace("\\.", "."))
            if detect is None:
                raise Exception(
                    "Existing component discovery with auto format, can not detect "
                    "file format. Please edit the format manually and rerun "
                    "migration. Affected component: {}/{}".format(
                        addon.component.project.slug, addon.component.slug
                    )
                )

            addon.configuration["file_format"] = detect.format_id
            addon.save()