MariaDB 10.5.8 上的 运行 Django 迁移问题

Issue When Running Django Migrations on MariaDB 10.5.8

我一直在尝试从 mysql 数据库(版本 5.7.26-29-31.37-log - SELECT VERSION() 的输出)迁移到 mariadb(版本 10.5.8 -MariaDB-log - 来自 SELECT VERSION())

的输出

此外,我看到 Django 从 2.2 版更新到 3.1.5 版,因为从 3.0 版开始正式支持 mariadb。有了这个,我也更新了 mysql 客户端库到 2.0.3.

但是当服务器上的“迁移”命令是 运行 时,它在使用新数据库时失败并出现此错误:decimal.InvalidOperation: []

这是堆栈跟踪:

DEBUG (0.008) SELECT @@SQL_AUTO_IS_NULL; args=None
DEBUG (0.007) SHOW FULL TABLES; args=None
DEBUG (0.007) SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; args=None
DEBUG (0.008) SHOW FULL TABLES; args=None
OUT Operations to perform:
OUT Apply all migrations: admin, auth, contenttypes, my_auth, essay, multichoice, quiz, sessions, sites, social_django, true_false
OUT Running pre-migrate handlers for application auth
OUT Running pre-migrate handlers for application contenttypes
OUT Running pre-migrate handlers for application sessions
OUT Running pre-migrate handlers for application sites
OUT Running pre-migrate handlers for application admin
OUT Running pre-migrate handlers for application quiz
OUT Running pre-migrate handlers for application multichoice
OUT Running pre-migrate handlers for application true_false
OUT Running pre-migrate handlers for application essay
OUT Running pre-migrate handlers for application my_auth
OUT Running pre-migrate handlers for application social_django
OUT Running migrations:
ERR [2021-01-06 09:32:31,188] (utils) DEBUG (0.007) SHOW FULL TABLES; args=None
ERR [2021-01-06 09:32:31,196] (utils) DEBUG (0.007) None; args=None
ERR Traceback (most recent call last):
ERR File "runapp.py", line 90, in <module>
ERR migrate()
ERR File "runapp.py", line 29, in migrate
ERR call_command("migrate", "-v 3", "--traceback")
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 168, in call_command
ERR return command.execute(*args, **defaults)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/core/management/base.py", line 371, in execute
ERR output = self.handle(*args, **options)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/core/management/base.py", line 85, in wrapped
ERR res = handle_func(*args, **kwargs)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 245, in handle
ERR fake_initial=fake_initial,
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 91, in migrate
ERR self.recorder.ensure_schema()
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 68, in ensure_schema
ERR editor.create_model(self.Migration)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 322, in create_model
ERR sql, params = self.table_sql(model)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 159, in table_sql
ERR definition, extra_params = self.column_sql(model, field)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 212, in column_sql
ERR db_params = field.db_parameters(connection=self.connection)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 718, in db_parameters
ERR check_string = self.db_check(connection)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 666, in db_check
ERR return connection.data_type_check_constraints[self.get_internal_type()] % data
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/utils/functional.py", line 48, in __get__
ERR res = instance.__dict__[self.name] = self.func(instance)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 344, in data_type_check_constraints
ERR if self.features.supports_column_check_constraints:
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/utils/functional.py", line 48, in __get__
ERR res = instance.__dict__[self.name] = self.func(instance)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/backends/mysql/features.py", line 104, in supports_column_check_constraints
ERR if self.connection.mysql_is_mariadb:
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/utils/functional.py", line 48, in __get__
ERR res = instance.__dict__[self.name] = self.func(instance)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 372, in mysql_is_mariadb
ERR return 'mariadb' in self.mysql_server_info.lower()
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/utils/functional.py", line 48, in __get__
ERR res = instance.__dict__[self.name] = self.func(instance)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 360, in mysql_server_info
ERR cursor.execute('SELECT VERSION()')
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 98, in execute
ERR return super().execute(sql, params)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 66, in execute
ERR return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
ERR return executor(sql, params, many, context)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute
ERR return self.cursor.execute(sql)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 73, in execute
ERR return self.cursor.execute(query, args)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/MySQLdb/cursors.py", line 206, in execute
ERR res = self._query(query)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/MySQLdb/cursors.py", line 321, in _query
ERR self._post_get_result()
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/MySQLdb/cursors.py", line 355, in _post_get_result
ERR self._rows = self._fetch_row(0)
ERR File "/home/vcap/deps/0/python/lib/python3.6/site-packages/MySQLdb/cursors.py", line 328, in _fetch_row
ERR return self._result.fetch_row(size, self._fetch_type)
ERR decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]

有人知道问题出在哪里吗?在我看来,调用 SELECT VERSION() 似乎会触发此转换错误。

谢谢!

最后,交换驱动程序/数据库连接器对我有用:

  1. 从依赖项中删除mysql客户端

  2. 添加mysql-connector-python~=8.0到依赖中(插入requirements.txt)

  3. 在 settings.py 文件

    中像这样调整您的 DATABASES 配置
    DATABASES = {
        'default': {
            'ENGINE': 'mysql.connector.django',
            'NAME': 'db_name',
            'USER': 'db_user',
            'PASSWORD': 'db_password',
            'HOST': 'db_host',
            'PORT': 3306,
            'OPTIONS': {
                    'autocommit': True,
                    'raise_on_warnings': True,
            },
        }
    }
    

希望对大家有所帮助!