Skip to content

Commit

Permalink
docs: Improve explanation of SQLAlchemy's scoped_session.
Browse files Browse the repository at this point in the history
  • Loading branch information
rbarrois committed Feb 6, 2015
1 parent f83c602 commit d95bc98
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions docs/orms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,16 @@ is to use `SQLAlchemy`_'s :class:`sqlalchemy.orm.scoping.scoped_session`:
:meth:`Session.remove <sqlalchemy.orm.scoping.scoped_session.remove>`
to reset the session.

.. note:: See the excellent :ref:`SQLAlchemy guide on scoped_session <sqlalchemy:unitofwork_contextual>`
for details of :class:`~sqlalchemy.orm.scoping.scoped_session`'s usage.

The basic idea is that declarative parts of the code (including factories)
need a simple way to access the "current session",
but that session will only be created and configured at a later point.

The :class:`~sqlalchemy.orm.scoping.scoped_session` handles this,
by virtue of only creating the session when a query is sent to the database.


Here is an example layout:

Expand Down Expand Up @@ -396,14 +406,14 @@ Here is an example layout:
def runtests():
engine = sqlalchemy.create_engine('sqlite://')
# It's a scoped_session, we can configure it later
common.Session.configure(engine=engine)
# It's a scoped_session, and now is the time to configure it.
common.Session.configure(bind=engine)
run_the_tests
- :class:`test cases <unittest.TestCase>` use this ``scoped_session``,
and clear it after each test:
and clear it after each test (for isolation):

.. code-block:: python
Expand Down

0 comments on commit d95bc98

Please sign in to comment.