Я создал это приложение CLI для инициализации db и et c. Я даже создал контекст приложения для 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 '