在 MariaDB 10.5.8 上运行 Django 迁移时出现的问题

问题描述

我一直在尝试从 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。有了这个,我也将 MysqLclient 库更新到了 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",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 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 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,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. 从依赖项中删除 mysqlclient

  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,},}
    }
    

我希望这对某人有所帮助!