Update CTE does not respect onupdate

Issue #3745 resolved
Jack Zhou created an issue

Test case:

class Foo(Base):
    __tablename__ = "foo"
    id = Column(Integer, primary_key=True)
    foo = Column(Integer, nullable=False)
    bar = Column(Integer, nullable=False, default=1, onupdate=2)

foo = Foo.__table__
session.execute(foo.insert().values(foo=1))
q = foo.update().values(foo=100).returning(foo.c.id)
session.execute(q)  # works
session.execute(select([q.cte("bar").c.id]))  # doesn't work

Log:

2016-07-12 11:42:34,641 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2016-07-12 11:42:34,641 INFO sqlalchemy.engine.base.Engine INSERT INTO foo (foo, bar) VALUES (%(foo)s, %(bar)s) RETURNING foo.id
2016-07-12 11:42:34,641 INFO sqlalchemy.engine.base.Engine {'foo': 1, 'bar': 1}
2016-07-12 11:42:34,642 INFO sqlalchemy.engine.base.Engine UPDATE foo SET foo=%(foo)s, bar=%(bar)s RETURNING foo.id
2016-07-12 11:42:34,642 INFO sqlalchemy.engine.base.Engine {'foo': 100, 'bar': 2}
2016-07-12 11:42:34,643 INFO sqlalchemy.engine.base.Engine WITH bar AS 
(UPDATE foo SET foo=%(foo)s, bar=%(bar)s RETURNING foo.id)
 SELECT bar.id 
FROM bar
2016-07-12 11:42:34,643 INFO sqlalchemy.engine.base.Engine {'foo': 100, 'bar': None}

Comments (3)

  1. Mike Bayer repo owner

    Work w/ prefetch even for selects, if present

    Fixed bug in new CTE feature for update/insert/delete stated as a CTE inside of an enclosing statement (typically SELECT) whereby oninsert and onupdate values weren't called upon for the embedded statement.

    This is accomplished by consulting prefetch for all statements. The collection is also broken into separate insert/update collections so that we don't need to consult toplevel self.isinsert to determine if the prefetch is for an insert or an update. What we don't yet test for are CTE combinations that have both insert/update in one statement, though these should now work in theory provided the underlying database supports such a statement.

    Change-Id: I3b6a860e22c86743c91c56a7ec751ff706f66f64 Fixes: #3745

    → <<cset e486ef666f39>>

  2. Log in to comment