在你改動了 model.py的內容之后執行下面的命令:
python manger.py makemigrations
相當於在該app下建立 migrations目錄,並記錄下你所有的關於modes.py的改動,比如0001_initial.py, 但是這個改動還沒有作用到數據庫文件
你可以手動打開這個文件,看看里面是什么。當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;
在makemigrations之后執行命令:
python manager.py migrate
將該改動作用到數據庫文件,比如產生table,修改字段的類型等。