【Django基础教程】makemigrations、migrate各种异常错误解决

makemigrations、migrate是用于同步models.py的数据库设计修改到数据库中

实际操作中可能遇到 makemigrations 执行成功

但是 migrate 部分成功、部分失败,也就是一部分修改同步到数据库,后面一部分因为其他原因而失败

这个时候,重复 makemigrations 和 migrate 操作都可以一直是失败。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[me@VM_0_14_centos video]# python manage.py makemigrations getvideo
Migrations for 'getvideo':
getvideo/migrations/0002_video_score.py
- Add field score to video

[me@VM_0_14_centos video]# python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, getvideo, sessions
Running migrations:
Applying getvideo.0002_auto_20190817_0709...Traceback (most recent call last):
File "/usr/local/lib64/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib64/python3.6/site-packages/django/db/backends/mysql/base.py", line 71, in execute
return self.cursor.execute(query, args)
File "/usr/local/lib64/python3.6/site-packages/MySQLdb/cursors.py", line 206, in execute
res = self._query(query)
File "/usr/local/lib64/python3.6/site-packages/MySQLdb/cursors.py", line 312, in _query
db.query(q)
File "/usr/local/lib64/python3.6/site-packages/MySQLdb/connections.py", line 224, in query
_mysql.connection.query(self, query)
MySQLdb._exceptions.OperationalError: (1054, "Unknown column 'score' in 'getvideo_video'")

这个时候我们得想到当前app下(getvideo)下有目录 migrations 缓存文件

暴力一点,我们把migrations/目录清空,重新执行 makemigrations和migrate

柔和一点,我们先看下操作记录:

1
[me@VM_0_14_centos video]# python manage.py showmigrations

然后migrations/ 下删除最近出错的几次操作缓存记录即可

坚持原创技术分享,您的支持将鼓励我继续创作!