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;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章