mysql-connector-python cursor_cent.py 文件上的 Django 迁移错误“TypeError:序列项 1:预期类似字节的对象,str found”

Posted

技术标签:

【中文标题】mysql-connector-python cursor_cent.py 文件上的 Django 迁移错误“TypeError:序列项 1:预期类似字节的对象,str found”【英文标题】:Django Migration Error 'TypeError: sequence item 1: expected a bytes-like object, str found' on mysql-connector-python cursor_cent.py file 【发布时间】:2021-12-22 07:06:09 【问题描述】:

我正在使用 mysql-connector 来处理 Django 项目中的 mysql 请求。问题是我正在使用“django-admin startproject project”设置一个简单的项目。当我尝试进行简单的 manage.py 迁移时,这是我的输出。

$ 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 shell 正确创建的 dbname 数据库。我还注意到,当我运行“python manage.py migrate”2 次时,在这个数据库上创建了两个表(我第一次运行它时,会发生上述错误,但我的数据库上没有创建表)。

我尝试在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,并且在 python venv 中完成所有这些工作,并通过 pip 安装了 django 和 mysql-connector-python。

【问题讨论】:

面临同样的问题 问题解决了吗? 不,我设法与 mysqlclient 一起工作......但这似乎坏了 刚刚使用 mysql-connector 也遇到了这个问题 - 可能是那里的错误。 【参考方案1】:

您是否为您的应用创建并定义了两个必需的文件views.py 和urls.py?

【讨论】:

欢迎来到 Stack Overflow。是什么让您认为此错误与缺少视图或 URL 有关?请确保您的答案确实解决了所提出的问题。见How to Answer。 是的,完成了【参考方案2】:

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

pip install mysql-connector-python<=8.0.26

【讨论】:

以上是关于mysql-connector-python cursor_cent.py 文件上的 Django 迁移错误“TypeError:序列项 1:预期类似字节的对象,str found”的主要内容,如果未能解决你的问题,请参考以下文章

我无法使用 pip 安装 mysql-connector-python

无法使用 mysql-connector-python 连接到 localhost,错误 1130

无法在 virtualenv 中安装 mysql-connector-python

安装python官方的mysql库“mysql-connector-python”

配置 .travis.yml 以支持 mysql-connector-python

ubuntu安装mysql-connector-python