我正在尝试在Flask应用程序中添加现有的MySQL数据库。
我的代码可以在下面找到:
__init__.py
:
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from config import config
db = SQLAlchemy()
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
db.init_app(app)
db.reflect(app=app)
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
return app
manage.py
:
from myapp import create_app
from flask.ext.migrate import Migrate, MigrateCommand
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)
migrate = Migrate(app, db)
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
models.py
是:
from . import db, login_manager
db.Model.metadata.reflect(db.engine)
class Calls(db.Model):
__table__ = db.Model.metadata.tables['Calls2']
__bind_key__ = 'calls'
def __repr__(self):
return self.test1
然而,我收到此错误:
Traceback (most recent call last):
File "./manage.py", line 9, in <module>
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
File "/home/ilias/client-reporting/app/__init__.py", line 49, in create_app
db.reflect(app=app)
File "/home/ilias/client-reporting/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 872, in reflect
self._execute_for_all_tables(app, bind, 'reflect')
File "/home/ilias/client-reporting/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 848, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), tables=tables)
File "/home/ilias/client-reporting/local/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 3380, in reflect
Table(name, self, **reflect_opts)
File "/home/ilias/client-reporting/local/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 406, in __new__
table._init(name, metadata, *args, **kw)
File "/home/ilias/client-reporting/local/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 473, in _init
self._extra_kwargs(**kwargs)
File "/home/ilias/client-reporting/local/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 552, in _extra_kwargs
self._validate_dialect_kwargs(kwargs)
File "/home/ilias/client-reporting/local/lib/python2.7/site-packages/sqlalchemy/sql/base.py", line 282, in _validate_dialect_kwargs
"named <dialectname>_<argument>, got '%s'" % k)
TypeError: Additional arguments should be named <dialectname>_<argument>, got 'tables'