On Feb 24, 2012, at 11:15 AM, Jonathan Vanasco wrote:

> I have a question about the Association Pattern
>     
> http://docs.sqlalchemy.org/en/latest/orm/relationships.html#association-object
> 
> I have a structure as such:
>    useraccount
>    group
>    useraccount_2_group ( useraccount_id , group_id , metadata like
> relation type / date / etc )
> 
> Is it possible to query the Groups based on the association ?
> 
> For example, in a webapp I have the useraccount_id from the session.
> I don't want to load the useraccount object  , i might just want the
> groups...
> 
>       """SELECT
>                       g.*
>               FROM
>                       useraccount_2_group u2g
>                       LEFT JOIN group g ON ( u2g.group_id = group.id )
>               WHERE
>                       u2g.useraccount_id = :useraccount_id"""
> 
> I guess I'm wondering if its possible to do:
>    dbSession.query( Group ).filter_or_join_or_whatever()

With association object, useraccount_2_group is mapped like any other object so 
you can use query()/filter()/join() normally across Group/UserAccountGroup 
without referring to UserAccount.

> 
> Similarly, I might want to access the 'relationship' as well.  From
> the docs, I don't understand if it would make more sense to load the
> Relationship and then eagerly pull in the Group -- or to use a join
> and grab a tuple [ 
> http://docs.sqlalchemy.org/en/latest/orm/tutorial.html#selecting-entities-from-subqueries
> ].  Does anyone have a preference for a reason ?

What to select and whether entities or columns generally has to do with what 
you want to do with the results.  If you're looking for plain tuples that are 
for displaying in a report, then I select columns.   if you want to get at the 
objects and navigate along relationships on those loaded objects then you'd 
load the whole object in.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to