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

Writing re-usable, decoupled models (multi-app architecture) #648

Closed
leonidumanskiy opened this issue Oct 17, 2018 · 4 comments
Closed

Writing re-usable, decoupled models (multi-app architecture) #648

leonidumanskiy opened this issue Oct 17, 2018 · 4 comments

Comments

@leonidumanskiy
Copy link

I am writing a complex web application that consists of a multiple flask apps, as well as non-flask Python apps (internal scheduler, etc).
I need to write models that I can re-use across multiple Flask apps, as well as non-flask python code using pure SA. However, I don't want to lose any benefits that Flask-SQLAlchemy gives you (when possible).

My first approach was to use declarative_base and it somewhat works:

from flask_sqlalchemy import declarative_base, DefaultMeta

Base = declarative_base(bind=user_db_conn_string, metaclass=DefaultMeta)

class User(Base):
    __bind_key__ = "user_db"
    name = Column(String(128), nullable=False)
    ....

However, multi-database binds don't seem to work properly. When I run

db.session.query(User).all()

It ignores the bind/bind key and runs it against the default database (and fails).

What should be correct approach here?

@leonidumanskiy leonidumanskiy changed the title Writing re-usable, decoupled models (microservices architecture) Writing re-usable, decoupled models (multi-app architecture) Oct 17, 2018
@ThiefMaster
Copy link
Contributor

Personally I just use Flask-SQLAlchemy everywhere. Even if it's not a webapp, you can simply wrap your code with a flask app context - all you need is to call db.init_app(app) and previously instantiate the app and set the sqlalchemy-related config.

@davidism
Copy link
Member

See #250 for previous work on this and https://stackoverflow.com/a/33127999/400617 for an example of what ThiefMaster is talking about.

@leonidumanskiy
Copy link
Author

Hi @davidism

I am really sorry but unfortunately, neither of those two links answer my question.

Example given in the end of #250 discussion does not work with a multi-database setup. Specifically, models do not bind to the correct engine using with __bind_key__ and I could not figure out how to make it bind properly.

Second link and solution provided by ThiefMaster allows to use models across multiple flask applications but it does not solve for non-flask environment. Shipping flask and flask-sqlalchemy with non-flask applications is barely a workaround and does not scale.

Imagine the following scenario - you have a non-flask codebase, developed by another team using SQLAlchemy, and you are developing a simple Flask-based frontend for it. Asking them to couple their architecture to flask-sqlalchemy and flask seems like a really bad idea.

@davidism
Copy link
Member

Please use Stack Overflow for questions about your own code. This tracker is for issues related to the project itself. Be sure to include a minimal, complete, and verifiable example.

@pallets-eco pallets-eco locked as off-topic and limited conversation to collaborators Oct 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

3 participants