On Sep 6, 2012, at 6:55 PM, Kuba Dolecki wrote:

> Hello,
> 
> I have been struggling for a few hours with the following issue. First, the 
> models:
> 
> stack_resources = db.Table('stack_resources',
>     db.Column('stack_id', db.Integer, db.ForeignKey('stack.id')),
>     db.Column('resource_id', db.Integer, db.ForeignKey('resource.id'))
> )
> 
> class Stack(BaseObject, db.Model):
>     id = some primary key
>     
>     resources = db.relation('Resource',
>         secondary=stack_resources,
>         secondaryjoin="and_(Resource.id==stack_resources.c.resource_id, \
>                      Resource.top_parent_resource==True)",
>         backref=db.backref('stacks'))
> 
> class Resource(BaseObject, db.Model):
>     id = some primary key
> 
>     top_parent_resource = db.Column(db.Boolean)
> 
> The issue lies with the query emitted. Here's the query for getting the 
> Resources associated with a stack:
> 
> SELECT ALL THE THINGS FROM RESOURCES
> FROM resource, stack_resources 
> WHERE %s = stack_resources.stack_id AND resource.id = 
> stack_resources.resource_id AND resource.top_parent_resource = %s
> 
> Basically, what appears to be missing is "AND stack.id = 
> stack_resources.stack_id". Anything I'm missing in the relationship 
> configuration that would make that happen?

if that's the "lazyload" query, meaning you have a Stack object loaded into 
memory and you're saying mystack.resources to access the "resources" 
collection, the query is correct.  You'll note the clause "%s = 
stack_resources.stack_id"' the primary key of your Stack object (which you'd 
see by saying "mystack.id") is passed as a bound parameter to the query and 
replaces where you see the %s.  You would also see the query in the parameter 
set which follows the SELECT statement if you're using echo=True on your Engine.



-- 
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