我们定义好model,执行两个命令Django 就帮我们迁移了数据库。这是因为我们使用了 Python 内置的 SQLite3 数据库。
SQLite3 是一个十分轻巧的数据库,它仅有一个文件。你可以看一到项目根目录下多出了一个 db.sqlite3 的文件,这就是 SQLite3 数据库文件
- python manage.py makemigrations
- python manage.py migrate
python manage.py makemigrations 后,Django 在 blog 应用的 migrations 目录下生成了一个 0001_initial.py 文件,这个文件是 Django 用来记录我们对模型做了哪些修改的文件。
E:\work2\blogproject>python manage.py makemigrations Migrations for 'blog': blog\migrations\0001_initial.py - Create model Category - Create model Post - Create model Tag - Add field tags to post
执行了 python manage.py migrate 命令。Django 通过检测应用中 migrations 目录下的文件,得知我们对数据库做了哪些操作,然后它把这些操作翻译成数据库操作语言,从而把这些操作作用于真正的数据库。
E:\work2\blogproject>python manage.py migrate Operations to perform: Apply all migrations: admin, auth, blog, contenttypes, sessions Running 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... OK
- python manage.py sqlmigrate blog 0001
查看sql脚本
E:\work2\blogproject>python manage.py sqlmigrate blog 0001 BEGIN; -- -- 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, "created_time" datetime NOT NULL, "modified_time" datetime NOT NULL, "e xcerpt" 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(100) 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_author_id_dd7a8485" ON "blog_post" ("author_id"); CREATE INDEX "blog_post_category_id_c326dbf8" ON "blog_post" ("category_id"); CREATE UNIQUE INDEX "blog_post_tags_post_id_tag_id_4925ec37_uniq" ON "blog_post_tags" ("post_id", "tag_id"); CREATE INDEX "blog_post_tags_post_id_a1c71c8a" ON "blog_post_tags" ("post_id"); CREATE INDEX "blog_post_tags_tag_id_0875c551" ON "blog_post_tags" ("tag_id"); COMMIT;
可以从根目录下的setting.py文件看到数据库连接。可以选择mysql也可以使用自带的sqlite3
# Database # https://docs.djangoproject.com/en/1.11/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } }