本文介紹了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
這篇關于Django遷移錯誤';TypeError:Sequence Item 1:需要一個類似字節的對象,但在MySQL-Connector-Python Cursor_cent.py文件上找到了字符串的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,