执行步骤:
python3 manger.py makemigrations
python3 manager.py migrate
说明:
1、改动了 model.py的内容之后执行第一步:
python manger.py makemigrations
相当于 在该app下建立 migrations目录,并记录下你所有的关于modes.py的改动,比如0001_initial.py, 但是这个改动还没有作用到数据库文件。你可以手动打开这个文件,看看里面是什么。
2、在此之后执行命令:
python manager.py migrate 将该改动作用到数据库文件,比如产生table之类。
3、当makemigrations之后产生了0001_initial.py 文件,你可以查看下该migrations会对应于什么样子的SQL命令
python3 manger.py sqlmigrate appname 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;