python manage.py makemigrations & migrate

一、正常流程

 1.python manage.py makemigrations 

 或 python manage.py makemigrations  appname

2. Python manage.py migrate

   python manage.py migrate appname

二、由于某种原因,人工删除库表,migrations文件等。

1.delete  django_migrations, django_content_type ,admin_log, auth_group, auth_group_permission,auth_permission

2.python  manage.py makemigrations appname

3.python manage.py migrate --fake appname

--fake  (由于人工操作,migration与实际table不一致,--fale 不会执行sql操作,只是对migration做标记,生成django_migrations表的内容。 重新一致。)
对于有数据的表,想保留数据,并且migrations一致,很重要 Tells Django to mark the migrations as having been applied
or unapplied, but without actually running the SQL to change your database schema. This is intended for advanced users to manipulate the current migration state directly if they’re manually applying changes;
be warned that using --fake runs the risk of putting the migration state table into a state where manual recovery
will be needed to make migrations run correctl

 另一个小实验记录:

比如music app,补充增加了一个表,但正常migrate后,增加表失败,这样处理

1.删除 music目录下的migrations

2.删除django_migrations表里面的,关于music app的records

3.删除music的表

重新migrations, migrate

三、其他系统的数据库,表和数据,想纳入django model框架管理,怎么办

参考:https://knivets.com/how-to-integrate-django-with-existing-database/

https://stackoverflow.com/questions/44470715/how-to-migrate-existing-table-using-django-and-python

python manage.py inspectdb
原文地址:https://www.cnblogs.com/lxgbky/p/12603302.html