回滾django的migration:
https://stackoverflow.com/questions/32123477/django-revert-last-migration
I've made a migration that added a new table and want to revert it and delete the migration, without creating a new migration. How do I do it? Is there a command to revert last migration and then I can simply delete the migration file? You can revert by migrating to the previous migration. For example, if your last two migrations are: 0010_previous_migration 0011_migration_to_revert Then you would do: ./manage.py migrate my_app 0010_previous_migration You can then delete migration 0011_migration_to_revert. If you're using Django 1.8+, you can show the names of all the migrations with ./manage.py showmigrations my_app To reverse all migrations for an app, you can run: ./manage.py migrate my_app zero
1、到數據庫表django_migrations中查看app中看看app列
2、到項目對應的app模塊中打開migrations文件查看生成的文件與數據庫app列中的是不是一樣
3.找到哪里不一致的文件,然后使用python manage.py --fake [版本名字]
,將這個版本標記為已經映射
如果還是報錯就按照下面執行
1、刪除指定app下migrations和數據庫表django_migrations中和這個app相關的版本號, 2、將模型中的字段和數據庫中的字段保持一致,再使用命令python manage.py makemigrations重新生成一個初始化的遷移腳本。 3、再使用命令python manage.py makemigrations --fake-initial來將這個初始化的遷移腳本標記為已經映射。之后再修改就沒有問題了。 更多關於遷移腳本的。請查看官方文檔:https://docs.djangoproject.com/en/2.0/topics/migrations/
makemigrations和migrate時django都做了什么?
makemigrations: 執行makemigrations時檢測models文件變化,在migrations文件夾中生成變更的sql的py文件 migrate:檢測django_migrations表,遷移過的表會記錄在其中並不再執行migrate,未找到執行記錄則進行migrate
不要隨意刪除django項目目錄下的 migrations文件夾,里面會記錄models文件每次makemigrations操作,誤刪后,makemigrations會重新生成
#執行python manage.py makemigrations命令時報錯問題及解決辦法: 在修改了models.py后,有些用戶會喜歡用python manage.py makemigrations生成對應的py代碼。 但有時執行python manage.py makemigrations命令(也可能人比較皮,把migrations文件夾給刪了),會提示"No changes detected." 可能有用的解決方式如下: 先 python manage.py makemigrations --empty yourappname 生成一個空的initial.py 再 python manage.py makemigrations 生成原先的model對應的migration file
django-關於manage.py migrate無效的問題
問題描述: 已有的model,修改之后,想重新建模,於是將migrations文件夾中除__init__.py之外其他文件都刪掉,再次執行以下步驟python manage.py makemigrations確認成功,執行python manage.py migrate,提示No migrations to apply. 表示一臉懵逼。再次修改,指定表名,再次嘗試,發現問題依舊,表示二臉懵逼 排查過程 python manage.py dbshell 進到數據庫里面,查看是否表已存在 結果:表不存在 檢查migrations文件 結果:文件沒問題 百度 google 各種搜,亂投醫,各種嘗試 解決方案 python manage.py dbshell 進到數據庫中,執行delete from django_migrations where app='your_appname'; python manage.py makemigrations(若migrations文件未刪除,可不執行這一步) python manage.py migrate 好啦,大功告成 原因分析 查看django_migrations表結構 建表語句: CREATE TABLE "django_migrations" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "app" varchar(255) NOT NULL, "name" varchar(255) NOT NULL, "applied" datetime NOT NULL); 原因 造成多次應用migrations失敗的原因是,當前model是修改過的,原來的migrations已經被我刪除,但是,重新生成的migrations使用遞增整數記名,所以,在django_migrations表中0001,0002等前面幾個數字的文件都已被記錄,在Django看來,被記錄了就相當於已應用,所以,會出現剛開始的No migrations to apply. 避免方案 有強迫症刪除migrations文件的同學(比如我),請同時到數據庫中刪除相應記錄 沒有強迫症的同學,可以繼續生成新的migrations,舊的就不必理會了 題外話 執行python manage.py migrate之后,可以使用python manage.py sqlmigrate appname migrations_num(例如python manage.py sqlmigrate user 0002)查看當前migrations文件對應的sql語句。 另外,在使用上述命令查看0002文件的sql語句時發現,django會新建一個表user_new,然后插入user表中的數據,再把user表刪掉,再把user_new重命名為user。所以,修改model的時候,不必擔心原有數據會丟失。
臨時解決:
在models中添加了一個model,makemigrations成功后,如果migrate提示no apply --》查看django——migrations是否有對應model的name記錄,如果沒有記錄 --》刪除model,再次makemigrations、migrate(提示無表,數據庫中手動創建對應表名)--》再添加model,再次makemigrations、migrate即可
上述問題出現的根因:項目目錄下面沒有留存/migrations/ 文件夾及遷移文件,導致,每次項目遷移記得保存好這個目錄的文件