执行
python3 manage.py makemigrations
报错如下
packages/Django-2.2-py3.7.egg/django/db/backends/mysql/features.py", line 82, in is_sql_auto_is_null_enabled
cursor.execute('SELECT @@SQL_AUTO_IS_NULL')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/Django-2.2-py3.7.egg/django/db/backends/utils.py", line 103, in execute
sql = self.db.ops.last_executed_query(self.cursor, sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/Django-2.2-py3.7.egg/django/db/backends/mysql/operations.py", line 146, in last_executed_query
query = query.decode(errors='replace')
AttributeError: 'str' object has no attribute 'decode'
解决方法如下(注意替换自己的django的位置)
1.
cd /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/Django-2.2-py3.7.egg/django/db/backends/mysql
2
vim operations.py
3
找到错误代码(line146):query = query.encode(errors='replace')
解决方法:把decode改为encode即可。
4保存并退出。
本文详细介绍了在使用Django进行数据库迁移时遇到的一个常见错误,即AttributeError: 'str' object has no attribute 'decode',并提供了具体的解决步骤。错误发生在尝试执行SQL查询时,解决方案涉及修改Django的mysql操作文件,将错误的decode调用更正为encode。
3184





