标签:
1.创建model,
2.然后再setting里面加入这个app,即激活模型
3.然后用命令:manage.py validate确认model是否有错误,
4.python manage.py makemigrations polls,建表,迁移文件,
5.python manage.py sqlmigrate book 0001用来运行步骤4的迁移文件,把django所要做的事情打印到屏幕上,并不改变数据库表,也可以运行python manage.py check命令;它能在你没有执行迁移或者没有接触到数据库时,检查你的项目中是否存在问题。
6.python manage.py migrate:运行migrate命令,在你的数据库中创建模型所对应的数据表:
步骤5生成的结果如下:
BEGIN;
CREATE TABLE "book_author" (
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"first_name" varchar(30) NOT NULL,
"last_name" varchar(40) NOT NULL,
"email" varchar(75) NOT NULL);
CREATE TABLE "book_book" (
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"title" varchar(100) NOT NULL,
"publication_date" date NOT NULL);
CREATE TABLE "book_book_authors" (
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"book_id" integer NOT NULL REFERENCES
"book_book" ("id"),
"author_id" integerNOT NULL REFERENCES
"book_author" ("id"),
UNIQUE ("book_id", "author_id"));
CREATE TABLE "book_publisher" (
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" varchar(30) NOT NULL,
"address" varchar(50) NOT NULL,
"city" varchar(60)NOT NULL,
"state_province" varchar(30) NOT NULL,
"country" varchar(50) NOT NULL,
"website" varchar(200) NOT NULL);
CREATE TABLE "book_book__new" (
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"title" varchar(100) NOT NULL,
"publication_date" date NOT NULL,
"publisher_id"integer NOT NULL REFERENCES
"book_publisher" ("id"));
INSERT INTO "book_book__new" ("publication_date", "publisher_id", "id", "title")
SELECT "publication_date", NULL, "id", "title" FROM "book_book";
DROP TABLE "book_book";
ALTER TABLE "book_book__new" RENAME TO "book_book";
CREATE INDEX "book_book_2604cbea" ON "book_book" ("publisher_id");
标签:
原文地址:http://my.oschina.net/yusi/blog/399076