On Feb 26, 2010, at 1:48 PM, Kent wrote:

> I'm certain sqlalchemy's got a function call in its guts that I was
> about to recreate from scratch, so I'm hoping you can spare me the
> trouble.
> 
> I'm trying to construct the foreign key where clause and from clause
> needed to populate a relation.
> 
> I'd explain how I got here, but might take several days, so instead,
> is there a function call to help me?
> 
> In other words, I've got an object, for example an order:
> 
> ===================================
> 
> orderdetail_table = Table("orderdetails",metadata,
>    Column("orderid", Unicode, ForeignKey('orders.orderid'),
> primary_key=True),
>    Column("lineid", Integer, primary_key=True),
>    Column("saleprice", Numeric, nullable=False),
>    Column("productid", Unicode(255),
> ForeignKey('products.productid'), nullable=False)
> )
> 
> product_table = Table("products", metadata,
>    Column("productid", Unicode(255), primary_key=True),
>    Column("brand", Unicode(255),
>    ...
> )
> 
> 
> 
> class Order(object):
>    pass
> 
> class OrderDetail(object):
>    pass
> 
> 
> # ---------------------------- OrderDetail
> -------------------------------------------------------- #
> orderdetail_mapper = mapper(OrderDetail, orderdetail_table,
> allow_null_pks=False,
>        properties=dict(product=relation(Product,
>                        cascade='refresh-expire,expunge', #don't save
> changes to Product
>                        lazy=False)))
> 
> 
> =====================
> 
> Say the 'product' relation is not populated on a *transient*
> OrderDetail object that I will not be issuing a session flush() for
> (there are errors detected.. but that's the long story).
> 
> I want to populate the transient OrderDetails 'product' attribute with
> the detached product.
> 
> I assume there is no way a refresh of the 'product' attribute will
> accomplish this since the parent obj is transient (which would really
> be what I want), so I am also assuming I'll need to build the pk
> clause and issue a session.query.get().
> 
> Since this is dynamic code (accepting any sqla object), I need to
> dynamically construct that pk clause and from clause based on the
> mapper's RelationProperty.  In other words, use _foreign_keys to
> construct this ?
> 
> But I imagine there is already a function call that will get me what I
> want.
> 
> In the end, for this example, I'd want to dynamically build
> session.query(Product).filter( * pk clause based on fks *)
> 
> Is there a function that can get me most everything I want (return the
> pk clause) or must I build that up myself, and if myself, do you
> recommend the RelationProperty's _foreign_keys attribute as the
> starting point?
> 
> Thanks in advance, again.

are you perhaps looking for sess.query(Product).with_parent(someorder) ?    
there's an example here:  
http://www.sqlalchemy.org/docs/mappers.html#building-query-enabled-properties



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

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

Reply via email to