一、正常流程
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