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

Creating flask-sqlalchemy models that are reusable #269

Closed
makmanalp opened this issue Mar 10, 2015 · 4 comments
Closed

Creating flask-sqlalchemy models that are reusable #269

makmanalp opened this issue Mar 10, 2015 · 4 comments

Comments

@makmanalp
Copy link

Hello, I have an interesting use case where I'm building a library of database models as well as a BaseModel and a BaseQuery that I plan on reusing in a bunch of different projects, e.g. from blahblah_core.models import User, but I'm struggling conceptually with what to use as my base class. In the library for the reusable models, do I use the flask-sqlalchemy Query and Model bases for all my stuff?

AFAIK, those require an instantiated flask.ext.sqlalchemy.SQLAlchemy(), even though that doesn't have to be tied to an app object yet. But the other project I'll be using these modules in will also have its own SQLAlchemy, right?

How do I structure this? Do I just give up and use the blahblah_core SQLAlchemy instance in the new project? Or is there a way of decoupling the flask-sqlalchemy extension instance from the Model and Query so that I can use those models with /any/ SQLAlchemy instance? Could I maybe just make them raw SQLAlchemy objects - but then how would I tie those objects in so that flask-sqlalchemy knows about them?

Thanks in advance,
~mali

@makmanalp
Copy link
Author

A gross way I can think of doing this is to wrap the model definitions in a function to form some kind of factory, where you pass in the SQLAlchemy instance you're using, which then creates your model classes using the given SQLAlchemy instance's Model and Query.

@makmanalp
Copy link
Author

After a bit more research, these seem like they could be useful:

"How to use flask-sqlalchemy with existing sqlalchemy model?" - http://stackoverflow.com/questions/19119725/how-to-use-flask-sqlalchemy-with-existing-sqlalchemy-model

"Document how to use plain SQLAlchemy models with the Flask-SQLAlchemy session" #98

https://github.com/mitsuhiko/flask-sqlalchemy/blob/master/flask_sqlalchemy/__init__.py#L639

@tony
Copy link

tony commented May 12, 2015

#250

@immunda
Copy link
Contributor

immunda commented Jun 9, 2015

Yep, overlaps with #250, closing.

@immunda immunda closed this as completed Jun 9, 2015
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 5, 2020
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