网络编程
位置:首页>> 网络编程>> Python编程>> 浅谈Django学习migrate和makemigrations的差别

浅谈Django学习migrate和makemigrations的差别

作者:萌鼠喝酸奶  发布时间:2021-05-11 03:36:11 

标签:django,makemigrate

本文主要研究的是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;

来源:http://blog.csdn.net/qq_28484355/article/details/77692773

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com