We have completed the design of the database, but only the Python language, and did not really create a database table. Translated into a database language, the real creation of database tables is implemented by Django Manage.py, the process of the term: migrating databases
- Switch to the directory where the manage.py is located, execute the command separately: Python manage.py makemigrations, python manage.py migrate
- Execute python manage.py makemigrations results
- Execute Python manage.py Migrate
-
f:\pythoncode\django\workspace\blogproject (djanoproject_env) Λpython manage.py Migrateoperations to perform:apply all migrations:admin, auth, blog, contenttypes, sessionsrunning Migrations:applyin G-contenttypes.0001_initial ... OK Applying auth.0001_initial ... OK Applying admin.0001_initial ... OK Applying Admin.0002_logentry_remove_auto_add ... OK Applying Contenttypes.0002_remove_content_type_name ... OK Applying auth.0002_alter_permission_name_max_length ... OK Applying auth.0003_alter_user_email_max_length ... OK Applying auth.0004_alter_user_username_opts ... OK Applying Auth.0005_alter_user_last_login_null ... OK Applying auth.0006_require_contenttypes_0002 ... OK Applying auth.0007_alter_validators_add_error_messages ... OK Applying auth.0008_alter_user_username_max_length ... OK Applying blog.0001_initial ... OK Applying sessions.0001_initial ... Okf:\pythoncode\django\workspace\blogproject (djanoproject_env) λ
- Command parsing
- Python manage.py makemigrations
- Similar to the creation of Linux folder migrations, the difference is that: here also create a file: 0001_initial.py. In other words, execute Python manage.py makemigrations create file: F:\pythoncode\django\workspace\blogproject\blog\migrations\0001_ initial.py
- 0001_initial.py role: Django is used to record what changes we have made to the model. For now, we have created 3 model classes in the models.py file, and Django has recorded these changes in the 0001_initial.py.
- python manage.py Migrate:
- to view the statements that actually create the database:
For someone who understands the database language, you can run the following command to see what Django has done for us:
Python manage.py sqlmigrate Blog 0001
You'll see the output of the Django-translated database table creation statement, which will help you understand how Django ORM works.
F:\pythoncode\django\workspace\blogproject (djanoproject_env) λpython manage.py sqlmigrate Blog0001BEGIN;----Create Model Category--CREATE TABLE"blog_category"("ID"Integer not NULL PRIMARY KEY AutoIncrement,"name"varchar (100) not NULL);----Create Model Post--CREATE TABLE"Blog_post"("ID"Integer not NULL PRIMARY KEY AutoIncrement,"title"varchar (70) not NULL,"Body"Text Not NULL,"Create_time"DateTime not NULL,"Modified_time"Date not NULL,"Excerpt"varchar () Not NULL,"author_id"Integer not NULL REFERENCES"Auth_User"("ID"),"category_id"integer not NULL REFERENCES"blog_category"("ID"));----Create Model Tag--CREATE TABLE"Blog_tag"("ID"Integer not NULL PRIMARY KEY AutoIncrement,"name"varchar (70) not NULL);----Add field tags to post--CREATE TABLE"Blog_post_tags"("ID"Integer not NULL PRIMARY KEY AutoIncrement,"post_id"integer not NULL REFERENCES"Blog_post"("ID"),"tag_id"Integer not NULL REFERENCES"Blog_tag"("ID")); CREATE INDEX"blog_post_4f331e2f"On"Blog_post"("author_id"); CREATE INDEX"blog_post_b583a629"On"Blog_post"("category_id"); CREATE UNIQUE INDEX"Blog_post_tags_post_id_4925ec37_uniq"On"Blog_post_tags"("post_id","tag_id"); CREATE INDEX"blog_post_tags_f3aa1999"On"Blog_post_tags"("post_id"); CREATE INDEX"BLOG_POST_TAGS_76F094BC"On"Blog_post_tags"("tag_id"); COMMIT; F:\pythoncode\django\workspace\blogproject
View Code
[Python] [Django Learning Article] [4]django Complete database code translation: Migrating Database (Migration)