1. 程式人生 > >【python】【django】migrate 和makemigrations的差別

【python】【django】migrate 和makemigrations的差別

在你改動了 model.py的內容之後執行下面的命令:

python manger.py makemigrations

相當於 在該app下建立 migrations目錄,並記錄下你所有的關於modes.py的改動,比如0001_initial.py, 但是這個改動還沒有作用到資料庫檔案

你可以手動開啟這個檔案,看看裡面是什麼

在此之後執行命令

python manager.py migrate

將該改動作用到資料庫檔案,比如產生table之類

當makemigrations之後產生了0001_initial.py 檔案,你可以檢視下該migrations會對應於什麼樣子的SQL命令

python manger.py sqlmigrate theapp 0001

大概是這個樣子的:

BEGIN;
CREATE TABLE "polls_choice" (
    "id" serial NOT NULL PRIMARY KEY,
    "choice_text" varchar(200) NOT NULL,
    "votes" integer NOT NULL
);
CREATE TABLE "polls_question" (
    "id" serial NOT NULL PRIMARY KEY,
    "question_text" varchar(200) NOT NULL,
    "pub_date" timestamp with time zone NOT NULL
);
ALTER TABLE "polls_choice" ADD COLUMN "question_id" integer NOT NULL;
ALTER TABLE "polls_choice" ALTER COLUMN "question_id" DROP DEFAULT;
CREATE INDEX "polls_choice_7aa0f6ee" ON "polls_choice" ("question_id");
ALTER TABLE "polls_choice"
  ADD CONSTRAINT "polls_choice_question_id_246c99a640fbbd72_fk_polls_question_id"
    FOREIGN KEY ("question_id")
    REFERENCES "polls_question" ("id")
    DEFERRABLE INITIALLY DEFERRED;

COMMIT;
--------------------- 
作者:柯可客 
來源:CSDN 
原文:https://blog.csdn.net/yang1z1/article/details/52235424 
版權宣告:本文為博主原創文章,轉載請附上博文連結!