码迷,mamicode.com
首页 > 数据库 > 详细

django创建数据库表方法

时间:2016-07-04 13:22:04      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

catcher:mysite catcher$ python manage.py makemigrations books
Migrations for ‘books‘:
  0001_initial.py:
    - Create model Author
    - Create model Book
    - Create model Publisher
    - Add field publisher to book
catcher:mysite catcher$ python manage.py sqlmigrate books 0001
BEGIN;
--
-- Create model Author
--
CREATE TABLE "books_author" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "first_name" varchar(30) NOT NULL, "last_name" varchar(40) NOT NULL, "email" varchar(254) NOT NULL);
--
-- Create model Book
--
CREATE TABLE "books_book" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "title" varchar(100) NOT NULL, "publication_date" date NOT NULL);
CREATE TABLE "books_book_authors" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "book_id" integer NOT NULL REFERENCES "books_book" ("id"), "author_id" integer NOT NULL REFERENCES "books_author" ("id"));
--
-- Create model Publisher
--
CREATE TABLE "books_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);
--
-- Add field publisher to book
--
ALTER TABLE "books_book" RENAME TO "books_book__old";
CREATE TABLE "books_book" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "title" varchar(100) NOT NULL, "publication_date" date NOT NULL, "publisher_id" integer NOT NULL REFERENCES "books_publisher" ("id"));
INSERT INTO "books_book" ("publication_date", "publisher_id", "id", "title") SELECT "publication_date", NULL, "id", "title" FROM "books_book__old";
DROP TABLE "books_book__old";
CREATE INDEX "books_book_2604cbea" ON "books_book" ("publisher_id");

COMMIT;
catcher:mysite catcher$ 

 

django创建数据库表方法

标签:

原文地址:http://www.cnblogs.com/persuit/p/5639908.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!