On Fri, Dec 8, 2017 at 3:29 AM, Tolstov Sergey <whistler...@gmail.com> wrote:
> I need to switch off changing (not new) database rows and write all changes
> to specific table on event "before_commit".
>
>  But when i try to use config.session.rollback() SqlAlchemy show me
>
> AttributeError: 'NoneType' object has no attribute 'transaction'
>
> I try config.session.expunge all instances on config.session.dirty and
> config.session.deleted but changes anyway write to database
>
> Can someone help me, how can i drop all transactions and write new on event
> before_commit ?

it's not possible to cancel the transaction within before_commit
unless you throw an exception - the event does not support a "silently
don't commit" feature.   It sounds like you are looking to not
actually .commit() at all.    The best solution would be if your
application has adequate abstraction on top of the ORM (e.g. an API,
such as a "transaction manager" or similar) such that when it normally
would seek to call .commit() on the Session, it does whatever else it
is that you need to do.     Short of that, you can make a subclass of
Session that overrides the Session.commit() method with your special
function.

If you override Session.commit() to receive a special exception, you
can have a before_commit that short-circuits the actual commit:

from sqlalchemy.orm import Session
from sqlalchemy import event


class MySession(Session):
    def commit(self):
        try:
            super(MySession, self).commit()
        except DontCommitException:
            print("Special don't commit thing")
            self.expire_all()


class DontCommitException(Exception):
    pass


@event.listens_for(MySession, "before_commit")
def _check_things(session):
    for obj in session.dirty:
        if obj.data == "dont_commit":
            raise DontCommitException("found a dont commit")


if __name__ == '__main__':
    from sqlalchemy import Column, Integer, String, create_engine
    from sqlalchemy.ext.declarative import declarative_base

    Base = declarative_base()

    class A(Base):
        __tablename__ = 'a'
        id = Column(Integer, primary_key=True)
        data = Column(String)

    e = create_engine("sqlite://", echo=True)
    Base.metadata.create_all(e)

    s = MySession(e)

    a1 = A(data='d1')
    s.add(a1)
    s.commit()

    a1.data = 'd2'

    s.commit()

    a1.data = 'dont_commit'
    s.commit()

    assert s.query(A.data).first() == ('d2', )









>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to