Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flask-SQLAlchemy integration does not work. #169

Open
ShigeruNakagaki opened this issue Jan 5, 2018 · 1 comment
Open

Flask-SQLAlchemy integration does not work. #169

ShigeruNakagaki opened this issue Jan 5, 2018 · 1 comment

Comments

@ShigeruNakagaki
Copy link

ShigeruNakagaki commented Jan 5, 2018

for example,
db is a SQLAlchemy instance of flask_sqlalchemy.
app is a Flask(name) instance of flask.
FlaskSQL class inherits SQL class of eve_sqlalchemy and the driver is db.

at db.init_app(app), there are mapping information in db.Model._decl_class_registry.data.
But driver.Model._decl_class_registry.data of FlaskSQL instance is empty. So, at line 297 in eve_sqlalchemy/__init__.py, it said KeyError because there is no mapping information.

  • added
    I found out what is wrong. Please do not define db = flask_sqlalchemy.SQLAlchemy() at eve_sqlalchemy/__init__.py. It works when I import db from eve_sqlalchemy/__init__.py for schema definition like
from eve_sqlalchemy import db

class Hoge(db.Model):
. . .

this code does not match with a tutorial.

thanks

@ajw0100
Copy link

ajw0100 commented Jan 19, 2018

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants