如何使用SQLalchemy和Alembic自动初始化数据库? [英] How can I initialize the database automatically with SQLalchemy and Alembic?

查看:73
本文介绍了如何使用SQLalchemy和Alembic自动初始化数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我跑步

$ flask db init
$ flask db migrate -m "initialization"
$ flask db upgrade

如果数据库不存在.我想在Python中运行此程序,例如像

if the database does not exist. I would like to run this within Python, e.g. something like

app.create_db()

这样我就不必在意设置数据库了.有可能吗?

so that I don't have to care about setting the database up. Is that possible?

我使用了 flask-sqlalchemy flask-migrations 插件

推荐答案

很显然,您已经安装了 flask-migrate,flask-sqlalchemy .

Obviously, you have installed flask-migrate, flask-sqlalchemy.

因此,您可以这样做:

from flask_sqlalchemy import SQLAlchemy
from flask import Flask

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = SQLAlchemy(app)
db.create_all()

API DOC:flask.ext.sqlalchemy.SQLAlchemy.create_all

但是您的问题使我感到困惑.为什么受 SQLAlchemy Alembic 限制?

but your Question has confused me. why restricted by SQLAlchemy and Alembic?

这篇关于如何使用SQLalchemy和Alembic自动初始化数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆