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

xiaoxiao2025-10-19  8

在你改动了 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; ---------------------  作者:柯可客  来源:  原文:https://blog.csdn.net/yang1z1/article/details/52235424  版权声明:本文为博主原创文章,转载请附上博文链接!

转载请注明原文地址: https://www.6miu.com/read-5038187.html

最新回复(0)