问题描述:
Django 。*需要MySQL 5.6或更高版本。它通过映射
DateTimeField
到打破了兼容性datetime(6)
。
在PyhCharm的命令行中执行python manage.py migrate命令时,抛了一个关于SQL的异常信息如下:
django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table ((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"))
在stack overflow上找到了发生此异常的原因:传送门,大概意思就是Django框架生成的SQL语句不兼容MySQL5.5,,,但是我既不想升级MySQL,也不想降级Django。其解决方法就是在setting.py顶部加入一段手动映射即可解决此问题。项目结构以及增加的代码如下:
截图:
代码:
from django.db.backends.mysql.base import DatabaseWrapper
DatabaseWrapper.data_types['DateTimeField'] = 'datetime' # fix for MySQL 5.5
添加之后再执行 python manage.py migrate,执行成功。
Nice!完美解决! 今晚加鸡腿。