comparison docs/contributing.rst @ 6639:2db16cda05ba

docs: clarify that Session usually should be called - methods should not be used directly Documentation based on clarification by Søren Løvborg: Session is the factory/singleton manager, which tracks the current session (per thread). To end the current session entirely and destroy the Session object, we call remove on the manager (Session.remove()). (A new session will be created on-demand.) Session() returns the current session for the active thread (or creates a new session, if there's none). commit is a method of the SQLAlchemy Session class, thus called as Session().commit() ... it's a method call on the current Session object, not the session factory/manager. SQLAlchemy may have some hackery to allow Session.commit() to be called, and the call automatically redirect to the actual Session object... but that's a hack and should be avoided. TL;DR: for remove, call it on Session; for everything else, call it on Session().
author Mads Kiilerich <mads@kiilerich.com>
date Sun, 14 May 2017 21:20:12 +0200
parents 3af2dea756db
children 813e1f9d9c53
comparison
equal deleted inserted replaced
6638:cfbc0d6860ca 6639:2db16cda05ba
180 180
181 Notes on the SQLAlchemy session 181 Notes on the SQLAlchemy session
182 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 182 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
183 183
184 Each HTTP request runs inside an independent SQLAlchemy session (as well 184 Each HTTP request runs inside an independent SQLAlchemy session (as well
185 as in an independent database transaction). Database model objects 185 as in an independent database transaction). ``Session`` is the session manager
186 and factory. ``Session()`` will create a new session on-demand or return the
187 current session for the active thread. Many database operations are methods on
188 such session instances - only ``Session.remove()`` should be called directly on
189 the manager.
190
191 Database model objects
186 (almost) always belong to a particular SQLAlchemy session, which means 192 (almost) always belong to a particular SQLAlchemy session, which means
187 that SQLAlchemy will ensure that they're kept in sync with the database 193 that SQLAlchemy will ensure that they're kept in sync with the database
188 (but also means that they cannot be shared across requests). 194 (but also means that they cannot be shared across requests).
189 195
190 Objects can be added to the session using ``Session().add``, but this is 196 Objects can be added to the session using ``Session().add``, but this is