Last active
April 12, 2023 07:28
-
-
Save TheBigRoomXXL/744f3806537cc9742ed5b7ca7220e9e3 to your computer and use it in GitHub Desktop.
Flask-SQLAchemy Session without expire_on_commit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" 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