On the Differences between Django learning migrate and makemigrations
- 2020-07-21 08:52:47
- OfStack
This paper mainly studies the differences between migrate and makemigrations in Django, as follows.
After you have changed the contents of model.py, execute the following command:
Python manger.py makemigrations
This is equivalent to creating the migrations directory under app and recording all your changes to modes.py, such as 0001_initial.py, but this change does not affect the database file
You can manually open this file and see what's inside
Execute the command after this
python manager.py migrate
Apply the change to a database file, such as generate table
When makemigrations generates the 0001_ES32en.py file, you can see what the migrations command looks like
python manger.py sqlmigrate theapp 0001
It looks something like this:
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;
conclusion
Above is the paper on Django learning migrate and makemigrations the difference between all content, hope to help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!