Skip to content

Instantly share code, notes, and snippets.

@TheBigRoomXXL
Last active April 12, 2023 07:28
Show Gist options
  • Save TheBigRoomXXL/744f3806537cc9742ed5b7ca7220e9e3 to your computer and use it in GitHub Desktop.
Save TheBigRoomXXL/744f3806537cc9742ed5b7ca7220e9e3 to your computer and use it in GitHub Desktop.
Flask-SQLAchemy Session without expire_on_commit
""" Usefull before 2.0 when serializing object after commit, now you should use *returning* """
from contextlib import contextmanager
# More details here: https://stackoverflow.com/a/51452451
@contextmanager
def no_expire():
"""Provide db.session with the expire_on_commit set to False only this time."""
s = db.session()
s.expire_on_commit = False
try:
yield
finally:
s.expire_on_commit = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment