Python/Go

Django_新建表/同步数据库失败问题

2018-12-18  本文已影响35人  古佛青灯度流年

起因

同步表的过程中,我手动将数据库中的一个表删除了,此时再去执行命令,发现不能再数据库中新建表了
修改了表结构以后执行python3 manage.py migrate 报错:

No changes detected

所以进数据库把对应的表删除了,想着重新生成这张表.
删除表以后执行:

python3 manage.py makemigrations
python3 manage.py migrate

还是不能生成表,提示:No changes detected

处理过程

$ rm -rf migrations/ __pycache__/

重新执行:

$ python3 manage.py makemigrations
No changes detected
$~/code/django/blogproject$ python3 manage.py makemigrations comments
Migrations for 'comments':
  comments/migrations/0001_initial.py
    - Create model Comment
$~/code/django/blogproject$ python3 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, blog, comments, contenttypes, sessions, users
Running migrations:
  No migrations to apply.
image.png
delete from django_migrations where app='yourappname';

重新执行生成数据库命令:

$ python3 manage.py makemigrations comments
No changes detected in app 'comments'
$~/code/django/blogproject$ python3 manage.py  migrate comments
Operations to perform:
  Apply all migrations: comments
Running migrations:
  Applying comments.0001_initial... OK

数据表顺利生成.

结论

在执行
python3 manage.py makemigrations
python3 manage.py migrate
操作的时候,不仅会创建0001_initial.py对应的模型脚本,还会创建一个数据库记录创建的模型.如果想重新生成数据库,需要三个地方都做删除.

上一篇 下一篇

猜你喜欢

热点阅读