Django迁移错误'TypeError:Sequence Item 1:需要一个类似字节的对象,但在MySQL-Connector-Python Cursor_cent.py文件上找到了字符串

我使用MySQL-Connector来处理Django项目中的MySQL请求。问题是,我正在使用django-admin startproject项目设置一个简单的项目。当我尝试执行简单的管理时,这是我的输出。

$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
aqui <mysql.connector.cursor_cext._ParamSubstitutor object at 0x7f408ae1ec10> b'SELECT engine FROM information_schema.tables WHERE table_name = %s'
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/core/management/base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 244, in handle
    post_migrate_state = executor.migrate(
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 91, in migrate
    self.recorder.ensure_schema()
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 68, in ensure_schema
    editor.create_model(self.Migration)
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 348, in create_model
    self.deferred_sql.extend(self._model_indexes_sql(model))
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 1087, in _model_indexes_sql
    output.extend(self._field_indexes_sql(model, field))
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 1106, in _field_indexes_sql
    if self._field_should_be_indexed(model, field):
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/db/backends/mysql/schema.py", line 111, in _field_should_be_indexed
    storage = self.connection.introspection.get_storage_engine(
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/mysql/connector/django/introspection.py", line 270, in get_storage_engine
    cursor.execute(
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 98, in execute
    return super().execute(sql, params)
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/mysql/connector/django/base.py", line 149, in execute
    return self.cursor.execute(query, new_args)
  File "/home/thejhoule/anon/anonproj/end/venv/lib/python3.8/site-packages/mysql/connector/cursor_cext.py", line 265, in execute
    stmt = RE_PY_PARAM.sub(psub, stmt)
TypeError: sequence item 1: expected a bytes-like object, str found

我已将设置正确配置为

DATABASES = {
    'default': {
        'NAME': 'dbname',
        'ENGINE': 'mysql.connector.django',
        'USER': 'XXXXX',
        'PASSWORD': 'XXXXX',
        'OPTIONS': {
            'autocommit': True
        }
    }
}

和通过MySQL外壳正确创建的数据库名。我还注意到,当我运行了两次之后,在这个数据库上创建了两个表(第一次运行时,出现了上述错误,但没有在我的数据库上创建表)。

我尝试处理stmt = RE_PY_PARAM.sub(psub, stmt)附近的代码,甚至尝试输入stmt.decode('utf-8')。然后,它会在同一点引发不同的错误

tmt = RE_PY_PARAM.sub(psub, stmt.decode('utf-8'))
TypeError: cannot use a bytes pattern on a string-like object

我使用的是Ubuntu 20.04,所有这些操作都是在一个pythonvenv中完成的,并通过pip安装了Django和mySQL连接器-python。


解决方案

这似乎是mysql-connector-python==8.0.27的错误,可以通过回滚一个版本来避免:

pip install mysql-connector-python<=8.0.26

相关文章