1. 程式人生 > 程式設計 >django遷移檔案migrations的實現

django遷移檔案migrations的實現

當模型models.py中發生改變時,即在models.py檔案操作資料表,使得資料庫中的表結構發生變化,需要使用命令,記錄這些操作,類似於日記。

修改完models.py後,先在Terminal下執行命令

python manage.py makemigrations

這是僅僅是生成遷移記錄,執行過後,資料庫中的表並未發生變化。若是models發生改變,但是執行以上命令,顯示的是

No changes detected

則需要執行

python manage.py makemigrations + 你所改變的models.py所在的目錄

然後,要想表結構發生變化,還要執行以下命令

python manage.py migrate

執行完畢後,資料庫中的表結構已經改變

補充知識:django使用migrations遷移版本和資料庫中報錯解決方案

回滾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列

django遷移檔案migrations的實現

2、到專案對應的app模組中開啟migrations檔案檢視生成的檔案與資料庫app列中的是不是一樣

django遷移檔案migrations的實現

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/ 資料夾及遷移檔案,導致,每次專案遷移記得儲存好這個目錄的檔案

以上這篇django遷移檔案migrations的實現就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。