如何解决Python3.6+Django2.2使用MySQL数据库时出现的系列问题

Liqurior_ppp 2018-08-17 09:26:44

settings.py 的 DATABASES属性
DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

'ENGINE': 'django.db.backends.mysql',
'HOST': '127.0.0.1',
'PORT': '3306',
'NAME': 'polls',
'USER': 'root',
'PASSWORD': 'root',
'OPTIONS': {
'init_command': 'SET sql_mode="STRICT_TRANS_TABLES"',
}
}
}


__init__.py 代码
import pymysql
pymysql.install_as_MySQLdb()

执行python manage.py makemigrations
显示 No Changes detected
执行python manage.py migrate
出错:

Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Traceback (most recent call last):
File "G:\Anaconda3\lib\site-packages\django-2.2.dev20180727211500-py3.6.egg\django\db\backends\utils.py", line 83, in _execute
return self.cursor.execute(sql)
File "G:\Anaconda3\lib\site-packages\django-2.2.dev20180727211500-py3.6.egg\django\db\backends\mysql\base.py", line 71, in execute
return self.cursor.execute(query, args)
File "G:\Anaconda3\lib\site-packages\pymysql\cursors.py", line 170, in execute
result = self._query(query)
File "G:\Anaconda3\lib\site-packages\pymysql\cursors.py", line 328, in _query
conn.query(q)
File "G:\Anaconda3\lib\site-packages\pymysql\connections.py", line 516, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "G:\Anaconda3\lib\site-packages\pymysql\connections.py", line 727, in _read_query_result
result.read()
File "G:\Anaconda3\lib\site-packages\pymysql\connections.py", line 1066, in read
first_packet = self.connection._read_packet()
File "G:\Anaconda3\lib\site-packages\pymysql\connections.py", line 683, in _read_packet
packet.check_error()
File "G:\Anaconda3\lib\site-packages\pymysql\protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "G:\Anaconda3\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax
to use near '(6) NOT NULL)' at line 1")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "G:\Anaconda3\lib\site-packages\django-2.2.dev20180727211500-py3.6.egg\django\db\migrations\recorder.py", line 55, in ensure_schema
editor.create_model(self.Migration)
File "G:\Anaconda3\lib\site-packages\django-2.2.dev20180727211500-py3.6.egg\django\db\backends\base\schema.py", line 314, in create_model
self.execute(sql, params or None)
File "G:\Anaconda3\lib\site-packages\django-2.2.dev20180727211500-py3.6.egg\django\db\backends\base\schema.py", line 134, in execute
cursor.execute(sql, params)
File "G:\Anaconda3\lib\site-packages\django-2.2.dev20180727211500-py3.6.egg\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "G:\Anaconda3\lib\site-packages\django-2.2.dev20180727211500-py3.6.egg\django\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "G:\Anaconda3\lib\site-packages\django-2.2.dev20180727211500-py3.6.egg\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "G:\Anaconda3\lib\site-packages\django-2.2.dev20180727211500-py3.6.egg\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "G:\Anaconda3\lib\site-packages\django-2.2.dev20180727211500-py3.6.egg\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "G:\Anaconda3\lib\site-packages\django-2.2.dev20180727211500-py3.6.egg\django\db\backends\utils.py", line 83, in _execute
return self.cursor.execute(sql)
File "G:\Anaconda3\lib\site-packages\django-2.2.dev20180727211500-py3.6.egg\django\db\backends\mysql\base.py", line 71, in execute
return self.cursor.execute(query, args)
File "G:\Anaconda3\lib\site-packages\pymysql\cursors.py", line 170, in execute
result = self._query(query)
File "G:\Anaconda3\lib\site-packages\pymysql\cursors.py", line 328, in _query
conn.query(q)
File "G:\Anaconda3\lib\site-packages\pymysql\connections.py", line 516, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "G:\Anaconda3\lib\site-packages\pymysql\connections.py", line 727, in _read_query_result
result.read()
File "G:\Anaconda3\lib\site-packages\pymysql\connections.py", line 1066, in read
first_packet = self.connection._read_packet()
File "G:\Anaconda3\lib\site-packages\pymysql\connections.py", line 683, in _read_packet
packet.check_error()
File "G:\Anaconda3\lib\site-packages\pymysql\protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "G:\Anaconda3\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
django.db.utils.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syn
tax to use near '(6) NOT NULL)' at line 1")

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "G:\Anaconda3\lib\site-packages\django-2.2.dev20180727211500-py3.6.egg\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "G:\Anaconda3\lib\site-packages\django-2.2.dev20180727211500-py3.6.egg\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "G:\Anaconda3\lib\site-packages\django-2.2.dev20180727211500-py3.6.egg\django\core\management\base.py", line 317, in run_from_argv
self.execute(*args, **cmd_options)
File "G:\Anaconda3\lib\site-packages\django-2.2.dev20180727211500-py3.6.egg\django\core\management\base.py", line 354, in execute
output = self.handle(*args, **options)
File "G:\Anaconda3\lib\site-packages\django-2.2.dev20180727211500-py3.6.egg\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "G:\Anaconda3\lib\site-packages\django-2.2.dev20180727211500-py3.6.egg\django\core\management\commands\migrate.py", line 204, in handle
fake_initial=fake_initial,
File "G:\Anaconda3\lib\site-packages\django-2.2.dev20180727211500-py3.6.egg\django\db\migrations\executor.py", line 91, in migrate
self.recorder.ensure_schema()
File "G:\Anaconda3\lib\site-packages\django-2.2.dev20180727211500-py3.6.egg\django\db\migrations\recorder.py", line 57, in ensure_schema
raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)
django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table ((1064, "You have an error in your SQL syntax; check the ma
nual that corresponds to your MySQL server version for the right syntax to use near '(6) NOT NULL)' at line 1"))
...全文
310 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Urila 2020-01-06
  • 打赏
  • 举报
回复
Django2.1 版本不再支持 MySQL5.5 更新升级一下 MySQL 版本就解决了。
DemSephiroth 2019-08-18
  • 打赏
  • 举报
回复
https://blog.csdn.net/weixin_30480583/article/details/99008811 https://blog.csdn.net/weixin_30480583/article/details/99008811 LZ参考这个帖子,亲测有用,最后query的decode改encode是在146行,在迁移前先创建对应的数据库

10,608

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 其他
社区管理员
  • 其他
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧