Open ShigeruNakagaki opened 6 years ago
I solved this by overriding the init_app
method of eve_sqlalchemy.SQL
which allows you to specify your own db
as driver
:
from eve import Eve
from eve.io.base import ConnectionException
from eve_sqlalchemy import SQL as _SQL
from eve_sqlalchemy.validation import ValidatorSQL
from my_models import db
class SQL(_SQL):
# https://github.com/pyeve/eve-sqlalchemy/blob/0.5.0/eve_sqlalchemy/__init__.py#L47-L55
def init_app(self, app):
try:
# FIXME: dumb double initialisation of the
# driver because Eve sets it to None in __init__
self.driver = db
self.driver.app = app
self.driver.init_app(app)
except Exception as e:
raise ConnectionException(e)
app = Eve(validator=ValidatorSQL, data=SQL)
for example,
db
is a SQLAlchemy instance offlask_sqlalchemy
.app
is a Flask(name) instance offlask
.FlaskSQL
class inheritsSQL
class ofeve_sqlalchemy
and the driver isdb
.at
db.init_app(app)
, there are mapping information indb.Model._decl_class_registry.data
. Butdriver.Model._decl_class_registry.data
of FlaskSQL instance is empty. So, at line 297 ineve_sqlalchemy/__init__.py
, it saidKeyError
because there is no mapping information.db = flask_sqlalchemy.SQLAlchemy()
ateve_sqlalchemy/__init__.py
. It works when I importdb
fromeve_sqlalchemy/__init__.py
for schema definition likeclass Hoge(db.Model): . . .