[Sqlalchemy-tickets] Issue #2983: Insert deleted object problem with after flush listeners (zzzeek/
Brought to you by:
zzzeek
|
From: Konsta V. <iss...@bi...> - 2014-03-04 11:09:37
|
New issue 2983: Insert deleted object problem with after flush listeners https://bitbucket.org/zzzeek/sqlalchemy/issue/2983/insert-deleted-object-problem-with-after Konsta Vesterinen: My guess is this problem is related to: https://bitbucket.org/zzzeek/sqlalchemy/issue/2501/the-delete-before-insert-problem Failing test case: ``` #!python import sqlalchemy as sa dns = 'sqlite:///:memory:' engine = sa.create_engine(dns) engine.echo = True connection = engine.connect() Base = sa.ext.declarative.declarative_base() class ModelA(Base): __tablename__ = 'a' id = sa.Column(sa.Integer, autoincrement=True, primary_key=True) name = sa.Column(sa.Unicode(255), nullable=False) class ModelB(Base): __tablename__ = 'b' id = sa.Column(sa.Integer, autoincrement=True, primary_key=True) name = sa.Column(sa.Unicode(255), nullable=False) Base.metadata.create_all(connection) Session = sa.orm.sessionmaker(bind=connection) session = Session() @sa.event.listens_for(sa.orm.session.Session, 'after_flush') def after_flush(session, flush_context): for obj in session: if not isinstance(obj, ModelA): continue b = session.query(ModelB).get(obj.id) if not b: b = ModelB(id=obj.id, name=u'b') session.add(b) else: b.name = u'updated b!' a = ModelA(name=u'A') session.add(a) session.flush() session.delete(a) session.flush() session.add(ModelA(id=a.id, name=u'A')) session.commit() b = session.query(ModelB).first() assert b.name == u'updated b!' ``` This also throws a warning (which I think it should not throw): SAWarning: Attribute history events accumulated on 1 previously clean instances within inner-flush event handlers have been reset, and will not result in database updates. Consider using set_committed_value() within inner-flush event handlers to avoid this warning. |