Skip to content
This repository was archived by the owner on May 7, 2024. It is now read-only.

Can you provide an example of how to commit and revert ? #5

Open
fwachs opened this issue Apr 13, 2016 · 4 comments
Open

Can you provide an example of how to commit and revert ? #5

fwachs opened this issue Apr 13, 2016 · 4 comments

Comments

@fwachs
Copy link

fwachs commented Apr 13, 2016

No description provided.

@dtheodor
Copy link
Owner

This is just an SQLAlchemy session object, you can use everything documented at SQLAlchemy's session documentation, which contains a lot of examples.

@fwachs
Copy link
Author

fwachs commented Apr 13, 2016

I understand.
Is it ok if I use it like this?

from flask_sqlalchemy_session import current_session
from contextlib import contextmanager

@contextmanager
def session_scope():
    s = current_session
    try:
        yield s
        s.commit()
    except:
        s.rollback()
        raise
def save(self, building):
        with session_scope() as current_session:
            current_session.add(building)
            return building.id

I assume flask-sqlalchemy-session will do the s.close() when the flask request is fulfilled. Right?

@dtheodor
Copy link
Owner

Yes. Also check http://docs.sqlalchemy.org/en/rel_1_0/orm/contextual.html

Btw, the s.rollback() you do might fail with an exception and eat up the original exception, you may want to ignore the new exception

@fwachs
Copy link
Author

fwachs commented Apr 13, 2016

Perfect!

About the s.rollback(), should I wrap it with try except pass ?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants