Get 'full' child when querying inherited tables

32 views
Skip to first unread message

pr64

unread,
May 24, 2012, 10:02:08 AM5/24/12
to sqlal...@googlegroups.com
Hi,

For some reason, I need to access my sqlite database through an API which encapsulates the sqlalchemy stuff.
In my api methods, the typical sequence is:
  1. get a session from the session maker
  2. query/commit the db using this session
  3. return the queried/commited object and close the session

From the caller side, the returned object may have expired but i don't care (I configure my session maker with expire_on_commit=False)

This works fine, ie the object is detached from the db but immediate attributes are available from reading without any call to a hidden sqlalchemy refresh.

If I want to access linked tables, I just implement it with joinedloads within my api and again the caller can read related attributes.

The problem comes with inherited tables. I would like to be able to tell sqlalchemy to automatically load the child instead of loading the base class (and after issueing a refresh). Here's a sample code: http://pastebin.com/SyKmh9Ac

Line 131 works fine since the requested object is a the base class object

Line 133 does not work since the requested object is a child class object

How can I configure sqa to be able to get the whole child without having later refreshes ?

Thanks a lot for your help,

Pierre

Michael Bayer

unread,
May 24, 2012, 10:45:50 AM5/24/12
to sqlal...@googlegroups.com
use the with_polymorphic() method of Query described at http://docs.sqlalchemy.org/en/rel_0_7/orm/inheritance.html#basic-control-of-which-tables-are-queried .   0.8 will have a more capable and flexible version of this function.

pr64

unread,
May 24, 2012, 11:34:50 AM5/24/12
to sqlal...@googlegroups.com
Worked perfectly. Thanks a lot for your quick answer and so great software.
Reply all
Reply to author
Forward
0 new messages