首先分享一下自己刚开始弄的错误的和(脑瓜疼):
Django项目的 models.py 的模板 ,Django 支持 sqlite3, MySQL, PostgreSQL等数据库,默认的是 sqlite3,需要setting.py设置
我开始就是想用 MySQL 数据库,结果我 Django是2.0的 ,Mysql是 5.5的,Django2.1不再支持MySQL5.5,必须5.6版本以上
还有就是 python的版本 ,python2 是Mysqldb, python3 是pymysql ,python3需要 在项目初始化的时候 用pymysql代替mysqldb使用
在迁移生成表时: python manage.py migrate
报错: 就是Django和mysql版本的问题
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 manual that corresponds to your MySQL server version for the right syntax to use near '(6) NOT NULL)' at line 1"))
解决办法:
二选一
(1)Django降级到2.0
pip install Django==2.0.0 -i https://pypi.douban.com/simple
(2)MySQL升级
习惯安装程序,下一步---下一步 就ok
推荐个我看的博客 https://blog.csdn.net/yancheng0527/article/details/83377401
版本没毛病的话!!!! 就简单了
models.py 文件
from
django.db
import
models
class
Person(models.Model):
name
=
models.CharField(max_length
=
30
)
age
=
models.IntegerField()
setting.py 文件
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'books', #你的数据库名称
'USER': 'root', #你的数据库用户名
'PASSWORD': '', #你的数据库密码
'HOST': '', #你的数据库主机,留空默认为localhost
'PORT': '3306', #你的数据库端口
}
}
python3的话 注意(py2的忽略):
在 项目的 初始化文件 __init__.py 中
import pymysql pymysql.install_as_MySQLdb()
然后完成了没毛病,ok ,在项目的路径下 输入这俩个命令就好了,
[root@localhost Django_ORM] # python manage.py makemigrations
[root@localhost Django_ORM] # python manage.py migrate
小白fightting !!!,