Nick Murdoch <nmurdoch <at> locayta.com> writes:

> I'm coming across difficulty while trying to find out which tags a user 
> has used for a particular page. At the moment I've had to resort to the 
> following code, but I'm sure this should be possible as a pure select() 
> statement or similar.
> 
> # page and user are instances of Page and User respectively.
> userstags = filter( 
>         lambda tag: page in tag.pages,
>         user.tags.distinct().orderBy(Tag.q.name)
> )

As Oleg says, today you'll have to construct the intermediate tables, 
something like:
tag_users = sqlbuilder.Table('tag_users')
page_tags = sqlbuilder.Table('page_tags')
user.tags.filter(AND(tag_users.user_id==User.q.id,
                     tag_users.tag_id== Tag.q.id,
                     Tag.q.id==page_tags.tag_id,
                     page_tags.page_id==Page.q.id,
                     Page.q.id==page.id)
 
I happened to spend a little time addressing a related issue over the weekend,
that led to allowing joins, foreignKeys, and instances to be used as shorthand
SQLBuilder expressions, so in the future (ie, assuming this is an unsurprising
syntax for those who have an opinion, and then once I get these changes into
trunk, in a release, etc):

user.tags.filter(AND(User.q.tags, # above AND lines 1,2
                     Tag.q.pages, # above AND lines 3,4
                     page))       # above AND line 5 

I think it addresses your concern pretty directly, probably the most confusing
thing is that there are more and more "equivalent" ways to express a query, in
this case because you can use either side of a join/fk so: 

user.tags.filter(AND(Tag.q.users, 
                     Page.q.tags, 
                     page))     

is identical. Confusing, or useful?

- Luke 


-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to