【发布时间】:2020-06-18 08:27:24
【问题描述】:
我已经创建了这个 CLI 应用程序来初始化 db 等。我什至已经为 db 创建了一个应用程序上下文,但是我遇到了一个错误。
请注意,我使用的是docker 和docker-compose。我知道这个错误与我们有多个应用程序并且数据库不知道要使用哪个应用程序的时间有关。但我不知道如何解决这个问题。
from click import group, command, option, pass_context
from sqlalchemy_utils import database_exists, create_database
from call.app import create_app
from call.extensions import db
from call.blueprints.user.models import User
# Create an app context for the database connection.
app = create_app()
db.init_app(app)
@group()
def cli():
""" Run SQLite3 related tasks. """
pass
@command()
@option('--with-testdb/--no-with-testdb', default=False,
help='Create a test db too?')
def init(with_testdb):
"""
Initialize the database.
:param with_testdb: Create a test database
:return: None
"""
with app.app_context():
db.drop_all()
db.create_all()
if with_testdb:
db_uri = '{0}_test'.format(app.config['SQLALCHEMY_DATABASE_URI'])
if not database_exists(db_uri):
create_database(db_uri)
return None
@command()
def seed():
"""
Seed the database with an initial user.
:return: User instance
"""
with app.app_context():
if User.find_by_identity(app.config['SEED_ADMIN_EMAIL']) is not None:
return None
params = {
'role': 'admin',
'email': app.config['SEED_ADMIN_EMAIL'],
'password': app.config['SEED_ADMIN_PASSWORD']
}
return User(**params).save()
@command()
@option('--with-testdb/--no-with-testdb', default=False,
help='Create a test db too?')
@pass_context
def reset(ctx, with_testdb):
"""
Init and seed automatically.
:param with_testdb: Create a test database
:return: None
"""
with app.app_context():
print(ctx)
ctx.invoke(init, with_testdb=with_testdb)
ctx.invoke(seed)
return None
cli.add_command(init)
cli.add_command(seed)
cli.add_command(reset)
这是错误:
/usr/local/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py:813: UserWarning: Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:///:memory:".
'Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. '
/usr/local/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py:834: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
/usr/local/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py:834: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
【问题讨论】:
标签: docker docker-compose click flask-sqlalchemy flask-login