Update object with results of a server-side processing [pgsql]

16 views
Skip to first unread message

Alexey Vihorev

unread,
Apr 1, 2013, 5:26:35 PM4/1/13
to sqlal...@googlegroups.com

Hi!

I’ve got a server-side trigger function (before insert) – it changes some fields of the inserted record, and I need this info back in my SA entity object (akin to what SA does with ID’s). SA uses RETURNING whenever it is supported, maybe I can use it as well? Or am I limited to refreshing the object manually via session.refresh()?

Thanks!

Michael Bayer

unread,
Apr 1, 2013, 8:34:56 PM4/1/13
to sqlal...@googlegroups.com
The insert() construct supports a call called returning() to emit whatever RETURNING you want, but if you're using the ORM, then the insert() construct is generated by your mappings.  In this case, the ORM right now favors being able to batch the INSERT statements together into an executemany(), which doesn't support RETURNING, so as far as it using RETURNING specifically for non primary-key defaults, the ORM isn't quite set up for that yet. 

But, that doesn't mean you can't get those values automatically.   Any column that's not passed in and is marked as having a "server default" will be queued up to fetch automatically as soon as you access it.  The FetchedValue construct is used as this marker when you're dealing with something like a trigger: 

from sqlalchemy import FetchedValue

class MyClass(Base):
   # ...

   some_col = Column(Integer, server_default=FetchedValue())



-- 
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 tosqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Alexey Vihorev

unread,
Apr 2, 2013, 5:40:46 AM4/2/13
to sqlal...@googlegroups.com
Wonderful! Thanks a lot.


2013/4/2 Michael Bayer <mik...@zzzcomputing.com>



--
Алексей Вихорев
Reply all
Reply to author
Forward
0 new messages