[Python] [Django Learning Article] [4]django Complete database code translation: Migrating Database (Migration)

Source: Internet
Author: User
Tags create index sessions

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

  1. 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
      •   
         for ' Blog ' :  blog\migrations\0001_initial.py:    - Create model Category    - Create Model Post     - Create model Tag    -Add field tags to post

    • 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) λ  


  2. 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:
      • But it just tells Django what changes we've made, in order for Django to really create a database table for us, and then execute the python manage.py migrate command. Django detects the files in the Migrations\ directory in the application, learns what we do with the database, and then translates these operations into the database operations language, which acts on the real database.
      • You can see the output of the command in addition to applying blog.0001_initial ... OK, Django also operates on other files. This is because, in addition to our own blog application, Django itself has a lot of apps built in, and these applications themselves need to store data. Can be in the settings.pyINSTALLED_APPsettings to see these apps, of course we don't have to worry about them at the moment.
      •  blogproject/settingspyinstalled_apps = [ ' django.contrib.admin '   ' Django.contrib.auth '  ' Django.contrib.contenttypes '  "django.contrib.sessions '  ' django.contrib.messages '   ' django.contrib.staticfiles '  ' blog '         

  3. 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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.