django mysql varchar_django怎样自动创建数据库table

这篇博客介绍了使用Django框架创建数据库表的步骤,包括`makemigrations`和`migrate`命令的使用。首先,通过`makemigrations`生成迁移文件,然后用`sqlmigrate`查看具体的SQL语句,最后执行`migrate`应用迁移,成功创建了Author、Book、Publisher模型,并为Book添加了publisher字段。
摘要由CSDN通过智能技术生成

87fefaf7b1a4fcaa1358e300c92e61d7.png

Qyouu

 django创建数据库表方法如下:123456789101112131415161718192021222324252627282930313233343536catcher:mysite catcher$ python manage.py makemigrations booksMigrations for 'books':  0001_initial.py:    - Create model Author    - Create model Book    - Create model Publisher    - Add field publisher to bookcatcher:mysite catcher$ python manage.py sqlmigrate books 0001BEGIN;---- 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$      catcher:mysite catcher$ sudo python manage.py migratePassword:Operations to perform:  Apply all migrations: admin, contenttypes, books, auth, sessionsRunning migrations:  Rendering model states... DONE  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 books.0001_initial... OK  Applying sessions.0001_initial... OKcatcher:mysite catcher$ 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值