Manage external declarative bases#250
Conversation
|
Can you make a test for this can perhaps document it a little? |
|
Overwriting |
|
It might be acceptable to just not overwrite Another issue is that this will blindly set Overall the idea is fine, it just needs some more work first. |
|
Hello All, I have been on an extended break, Over the coming days I will correctly implement this patch, write the tests, and make the required modifications for this to be pulled. |
|
Great, look forward to it! 👍 |
|
Good progress, taking @davidism's advice, I no longer override Instead, registering a base will add it to reference in For convenience, I have added a property called This pull requests supports an arbitrary number of bases, since all bases, including the internal are available as an iter property ( I have added some additional test cases for this pull request, however I have disabled one, as I cannot find a solution to fix this minor problem when using raw SQLA. - When using a relationship (dynamically mapped), the query class is SQLA's own (obviously), however I am unable to re-construct this at runtime to patch it to use Flask-SQLA's own patched BaseQuery with convenience methods (pagination, etc). Only minor for now, but is something worth pointing out. I believe a solution for this is to set the session's default query class, however I did not want to do this as this may have unwanted side affects. Hopefully we can get this merged soon, cloning my fork via pip makes me nervous every time :P 👍 |
|
Is this going to be merged? It would be great to be able to use raw sqlalchemy models with flask |
|
Hi, find some problem with this patch:
As you can see, the |
|
@pahaz, The way this patch is implemented is that it works together with the Flask-Sqlalchemy's builtin declarative base - It does not actually replace the original base. This means you can use a hybrid of both SQLA models and Flask-SQLA models. Additionally, you can register multiple bases. Hence when needing to access the sorted_tables property, you must do this via each of your declarative bases individually. Alternatively, a property which returns all the bases registered can be found on |
|
I think migrations need to be split up per metadata for Alembic anyway. That should probably be something Flask-Migrate handles, like it handles tying in the one metadata right now. |
|
I agree with @davidism on that point |
|
Yep Houston we have a problem. I need this. I love flask, but I'm missing the ability to scan and inject the connection data into the objects... @nickw444 You still on this? I'm going to take a look at how django / django orm does it, then see how this compares. |
|
@tony I'm still on this, we're just waiting for this to be pulled back. For the time being my fork works flawlessly, I'm using it in about 5 projects |
|
@nickw444 Can you rebase / squash? I'm going to take a deeper look and give it a QA |
|
@pahaz I recently needed to use Flask-Migrate with a project with this setup with models. In the alembic |
|
@tony Are you reviewing on behalf of @mitsuhiko / other devs of this repo? I'll squash now if that's the case. Thanks! |
|
@immunda ETA on this being merged/v3? Also am I squashing the changes? |
|
Just an update: have been using this for a while now and no trouble so far. |
|
Any update on when this might get merged? I'd love to be able to remove my models' dependencies on a single module-level "db" (and all the initialization and testing headaches that it brings) and just use plain SQLAlchemy models. |
|
I would also like to know when this is being merged |
|
I'll be releasing |
|
Thank you! |
|
+1 on this. I currently have to keep two copies of the same models-- one for SQLAlchemy and one for Flask-SQLAlchemy. Merging this would remove a huge headache. Thanks for doing this @nickw444. |
|
@immunda any news on the 2.1 release? i've also recently run into this problem. i'm using scrapy to scrape data and store models into db via sqlalchemy. later i use flask to serve up the models. scrapy shouldn't/doesn't need to be tied into flask. i want to use the sqlalchemy models in both places. poked around, but haven't seen any 2.1 yet. thanks! |
|
I'd like to use this but am wary of jumping in if the migration / alembic story isn't trustworthy. Is there an Eta for merge and maybe the next flask-sqlalchemy release? |
|
There is no ETA. There is also nothing "untrustworthy" about using Flask-SQLAlchemy / Alembic / other base models right now, you're just not going to get automatic integration with Flask-Migrate (which we don't control anyway) or some of the helpers from Flask-SQLAlchemy in your external base. External bases can already be patched in to the |
|
@nzjrs from my experience (running this PR for over a year and a half now in production), I've never had any issues with Alembic. I use flask-migrate the normal way, and just add |
|
@nickw444 sadly this PR will not work with multiple applications safely due the query class set to an application specific one. Same applies to the query attribute. |
|
@mitsuhiko this PR doesn't change the query class, unless I'm overlooking something. |
|
@davidism looking at the code |
|
But those are extension specific, not app specific. Same with the model, metadata, session class, etc. |
|
That however then means that you have to use the model with that instance of the extension only. If that is intended than a security check with an assertion should be added. People should not assume that this change means they can make one instance of this extension per app then. |
|
Ah, ok. There's probably a way to make this work. |
|
Hello, I am facing this exact problem. I auto generated an existing Mysql schema using Sqlacodegen, but the thing is, I wish to use Flask-SQLAchemy instead pure SQLAlchemy. This patch would be really helpful. I will try to look at it and see if I can contribute. |
|
If you need it today, you can manually patch it in by overriding one class. I have posted an example to SO here: http://stackoverflow.com/questions/28789063/associate-external-class-model-with-flask-sqlalchemy The only complication I have had is with flask-migrate. I had to change the generated env script to pull from the metadata on the external base. |
* Adds models using plain SQLAlchemy since pallets-eco/flask-sqlalchemy#250 looks like it won't be merged soon. Signed-off-by: Diego Rodriguez <diego.rodriguez@cern.ch>
* Adds models using plain SQLAlchemy since pallets-eco/flask-sqlalchemy#250 looks like it won't be merged soon. Signed-off-by: Diego Rodriguez <diego.rodriguez@cern.ch>
* Adds models using plain SQLAlchemy since pallets-eco/flask-sqlalchemy#250 looks like it won't be merged soon. Signed-off-by: Diego Rodriguez <diego.rodriguez@cern.ch>
* Adds models using plain SQLAlchemy since pallets-eco/flask-sqlalchemy#250 looks like it won't be merged soon. Signed-off-by: Diego Rodriguez <diego.rodriguez@cern.ch>
|
My team would also like to be able to define SQLAlchemy models in a separate package from the flask app and import the package into both Flask based and applications without a Flask dependency. This seems to be a commonly requested feature. @mitsuhiko, is there a better approach that you can recommend to enable using SQLAlchemy models that were not defined extending a Flask session's base model? |
|
It's possible to use SqlAlchemy models outside a flask app; I have been using it! I used sqlalchemy Maybe we can extend the documentation to include this? It took me some time to get this to work,
Using outside of flask requires a few simple steps. I keep those in a dedicated
|
|
Did anything come from this? Three years of development and discussion and then it was closed? |
@gtalarico's comment above is a code example for how to do this today |
Fix for Issue #98