Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Why is session part of the declarative API? #11
My SQL Alchemy models are totally decoupled from ways to access them (so from both an engine and a session). My session objects are created depending on the python process that makes use of the models (for instance, I am using a
This makes it impossible to use this library. Why is it a requirement to pass the session to the model declaration?
As of #8,
Alternatively, would it help if
@jmcarp a callable would help but being able to pass it to the constructor would be best. When you're not using a thread local SQLA session (such as with gevent servers) you need to be able to pass the session.
@dtheodor if you need a stop gap just set sqla_session to object(), as long as you don't have marshmallow getting related fields then nothing will break.
In the spirit of how a session is used in SQL Alchemy (which is that the session knows about the models, the models know not of the session), the right place (from the API perspective) for it to be passed would be in the dumps/loads methods:
author = author_schema.load(author_data, session=session) session.add(author) session.commit()
I really can't think of any other nice way for this (and is also the only way to support more "advanced" features like more than one session). My alternative is to put all the marshmallow schemas in a factory function that receives a session and declares the schemas with the correct session.
The callable solution would work but would require a lot of hackery (would have to create some global config object that points to the right session factory depending on some env. variable or something like that. So my models would not be coupled to a session but to a clunky config setup. I'd rather stay away from this and go with the schema factory method)
My project uses Flask, SQLAlchemy + pyMysql and Marshmallow/marshmallow-sqlalchemy.
I implemented the declarative way to combine flask and SQLAlchemy.
There is a section
To close the scoped session after every requset.
With sqla_session, I believe there will be two scoped sessions got involved in a request, one to the the .add()/.merge() job, and the other one within schema.
But the inside one, the sqla_session, remains in the connection pool, but it got expired and closed by the server. So after idle for a while, I got
I believe this problem would be solved by removing the
Thanks the issue got fixed.