即使设置数据库URL,我也会收到错误消息。错误消息是这样的。
Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:///:memory:"
这是我的app.py
from flask import Flask, Blueprint
from flask_alembic import Alembic
from flask_restplus import Resource
from werkzeug.contrib.fixers import ProxyFix
from flask_sqlalchemy import SQLAlchemy
from config import config_by_name
from .apis import api
db = SQLAlchemy()
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config_by_name[config_name])
app.wsgi_app = ProxyFix(app.wsgi_app)
app.config['BUNDLE_ERRORS'] = True
db.init_app(app)
api.init_app(app)
alembic = Alembic()
alembic.init_app(app)
blueprint = Blueprint('api', __name__, url_prefix='/api')
app.register_blueprint(blueprint)
return app
当我运行create_app()时,它将获取配置名称并设置配置。这是我的config.py
class DevelopmentConfig(Config):
"""
config for develop
"""
DEBUG = True
TESTING = True
SQLALCHEMY_DATABASE_URI = 'mysql://root:dev@localhost:3306/develop'
PRESERVE_CONTEXT_ON_EXCEPTION = False
SQLALCHEMY_TRACK_MODIFICATIONS = False
config_by_name = dict(
dev=DevelopmentConfig )
还有其他环境配置,但我将其删除。 这是run.py
app = create_app('dev')
if __name__ == '__main__':
from config import SERVER_HOST, SERVER_PORT
app.run(host=SERVER_HOST, port=SERVER_PORT, debug=True)