[ORM] Fetching columns that were assigned a SQL expression as part of INSERT...RETURNING and UPDATE...RETURNING

15 views
Skip to first unread message

univerio

unread,
Apr 28, 2015, 7:35:34 PM4/28/15
to sqlal...@googlegroups.com
Suppose I have a PostgreSQL backend and I have the following class:

    class Foo(Base):
        id = Column(Integer, primary_key=True)
        updated_at = Column(DateTime)

and I do

    foo = Foo(updated_at=func.now())
    session.add(foo)
    session.flush()
    foo.id  # this is already loaded, no additional query emitted
    foo.updated_at  # this is not loaded, will cause an additional query to be emitted

Is it possible to have the SQLAlchemy ORM fetch the actual value of updated_at as part of the INSERT...RETURNING statement like it does for id, instead of leaving it unloaded and having to issue a second query when I access it?


Jack

Mike Bayer

unread,
Apr 28, 2015, 7:42:14 PM4/28/15
to sqlal...@googlegroups.com
--
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+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Jack Zhou

unread,
Apr 28, 2015, 9:26:22 PM4/28/15
to sqlal...@googlegroups.com
Great! That worked, though I had to add server_default=FetchedValue() and server_onupdate=FetchedValue(). Would be nice if SQLAlchemy auto-detected this case, but no big deal otherwise.


Thanks!

Jack

You received this message because you are subscribed to a topic in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/F2QKToSNKKM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sqlalchemy+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages