標籤:技術分享 size message import pre float margin running mod
上一篇我們已經完成資料庫的設計,但是僅僅是python語言,並沒有真正建立了資料庫表。翻譯成資料庫語言,真正建立資料庫表由django manage.py來實現,這一過程專業術語:遷移資料庫
- 切換到manage.py所在目錄,分別執行命令:python manage.py makemigrations , python manage.py migrate
- 執行python manage.py makemigrations結果
- 執行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: Applying 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) λ
- 命令解析
- python manage.py makemigrations
- 類似於linux的建立檔案夾migrations,不同地方在於:這裡還建立一個檔案:0001_initial.py。換言之,執行python manage.py makemigrations 建立檔案: F:\pythoncode\django\workspace\blogproject\blog\migrations\0001_initial.py
- 0001_initial.py作用: Django 用來記錄我們對模型做了哪些修改的檔案。目前來說,我們在 models.py 檔案裡建立了 3 個模型類,Django 把這些變化記錄在了 0001_initial.py 裡。
- python manage.py migrate :
- 不過此時還只是告訴了 Django 我們做了哪些改變,為了讓 Django 真正地為我們建立資料庫表,接下來又執行了
python manage.py migrate 命令。Django 通過檢測應用中 migrations\ 目錄下的檔案,得知我們對資料庫做了哪些操作,然後它把這些操作翻譯成資料庫操作語言,從而把這些操作作用於真正的資料庫。
- 可以看到命令的輸出除了 Applying blog.0001_initial... OK 外,Django 還對其它檔案做了操作。這是因為除了我們自己建立的 blog 應用外,Django 自身還內建了很多應用,這些應用本身也是需要儲存資料的。可以在 settings.py 的
INSTALLED_APP 設定裡看到這些應用,當然我們目前不必關心這些。
blogproject/settings.pyINSTALLED_APPS = [ ‘django.contrib.admin‘, ‘django.contrib.auth‘, ‘django.contrib.contenttypes‘, ‘django.contrib.sessions‘, ‘django.contrib.messages‘, ‘django.contrib.staticfiles‘, ‘blog‘,
- 查看真正建立資料庫的語句:
對於瞭解資料庫語言的人,你可以運行下面的命令看看 Django 究竟為我們做了什麼:
python manage.py sqlmigrate blog 0001
你將看到輸出了經 Django 翻譯後的資料庫表建立語句,這有助於你理解 Django ORM 的工作機制。
F:\pythoncode\django\workspace\blogproject(djanoproject_env) λ python manage.py sqlmigrate blog 0001BEGIN;---- 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(200) 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\blogprojectView Code
[python][django學習篇][4]django完成資料庫代碼翻譯:遷移資料庫(migration)