首先创建数据库了,打开终端,转到manage.py文件所在的文件夹,然后运行一下命令:
python manage.py makemigrations
显示如下输出内容:
Migrations for 'blog': blog/migrations/0001_initial.py - Create model BlogArticles
迁移文件将被翻译成SQL语句。执行下面命令:
python manage.py sqlmigrate blog 0001
然后可以看到如下输出内容:
BEGIN;
---- Create model BlogArticles--CREATE TABLE "blog_blogarticles" ("id" integer NOT PRIMARY KEY AUTOINCREMENT, "title" varchar(300) NOT , "body" text NOT , "publish" datetime NOT , "author_id" integer NOT REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED);
CREATE INDEX "blog_blogarticles_author_id_ed798e23" ON "blog_blogarticles" ("author_id");
COMMIT;最后我们将生产的迁移文件应用到数据库中:
python manage.py migrate