目前,我运行
$ flask db init
$ flask db migrate -m "initialization"
$ flask db upgrade
如果数据库不存在。我想在 Python 中运行它,例如就像是
app.create_db()
这样我就不必关心设置数据库了。那可能吗?
我用flask-sqlalchemy
and flask-migrations
plugins
您可以使用SQLAlchemy 实用程序 https://pypi.org/project/SQLAlchemy-Utils/为了这。
from sqlalchemy import create_engine
from sqlalchemy_utils import database_exits,create_database
def validate_database():
engine = create_engine('postgres://postgres@localhost/name')
if not database_exists(engine.url): # Checks for the first time
create_database(engine.url) # Create new DB
print("New Database Created"+database_exists(engine.url)) # Verifies if database is there or not.
else:
print("Database Already Exists")
在 __init__.py 文件中调用此方法,以便它在每次服务器启动时进行检查。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)