Select from an inherited table in TurboGears (Since it took me so long to figure this out...)


Okay, key tip for debugging TurboGears SQLObject problems, add ?debug=1 to the dev.cfg URL for your database. Once you have that and you try to run a query for an inherited class you'll see the problem, but so that you don't have to set it up, here's the description of the problem and the solution:

Suppose you have 2 classes, Node(InheritableSQLObject) and Package(Node), and you want to get all Packages that are owned by the user with ID 9, where "owner" is a field on Node. You might write code like this:
Package.select( Node.owner == myUser )

But you would then be a very sad coder! SQLObject will silently turn this into the not particularly helpful query "... WHERE (('f') and ...)" that is, a query that can never return anything.

Turns out that you have to use the "q" namespace within your class to make the selection work. So far I've also had to use the ID, rather than a User object, as "owner" doesn't seem to show up in the Node namespace. So, your working selection code would look like this:
Package.select( Node.q.ownerID == myUser.id )

With that you get the expected set of Package objects belonging to the user. And there we are, right where I expected to be... well... quite a while ago now (yesterday sometime), with the list of packages to display to the customer. Yay.

Comments

Comments are closed.

Pingbacks

Pingbacks are closed.