I'm pretty sure that I can use RelationProperty.synchronize_pairs
to tell me which fks I need on the parent.

To determine the class of the relation, I've worked this pseudo-code
out, but if there is a nicer way, please tell me:

if isinstance(RelationProperty.argument, Mapper):
  cls = RelationProperty.argument.class_  #for backrefs, apparently
else:
  cls = RelationProperty.argument

To get the order of the ident keys to pass to session.query().get():

class_mapper(cls).primary_key


Seems there might be an easier way, I'd very much like to hear it.



On Feb 26, 3:31 pm, Kent <[email protected]> wrote:
> Also, it is feasible (or likely even) that the relation I am
> attempting to query is already persistent in the session.  Is .get()
> the only function that attempts to find an unexpired persistent
> instance before going to the database?
>
> Will doing a session.query().filter() also check if the object is
> persistent before querying the database (doubt that's possible?)?
>
> On Feb 26, 3:23 pm, Kent <[email protected]> wrote:
>
> > It *sounds* like exactly what I'm looking for, but when I try a simple
> > example, it doesn't seem to work:
>
> > >>> ol=OrderDetail()
> > >>> ol.productid = 'DININGCHAIR'
> > >>> DBSession.query(Product).with_parent(ol).all()
>
> > 15:21:57,688 INFO  [sqlalchemy.engine.base.Engine.0x...d3d0] BEGIN
> > 15:21:57,694 INFO  [sqlalchemy.engine.base.Engine.0x...d3d0] SELECT
> > products.productid AS products_productid, products.brand AS
> > products_brand, products.vendorsku AS products_vendorsku,
> > products.stockeditem AS products_stockeditem,
> > products.packagesplittype AS products_packagesplittype,
> > products.packagecomponent AS products_packagecomponent,
> > products.productgroup AS products_productgroup, products.productfamily
> > AS products_productfamily, products.productsubfamily AS
> > products_productsubfamily, products.description AS
> > products_description, products.regular AS products_regular,
> > products.commissiontype AS products_commissiontype,
> > products.replacementcost AS products_replacementcost, products.sale AS
> > products_sale, products.onhand AS products_onhand, products.onorder AS
> > products_onorder, products.imageurl AS products_imageurl,
> > products.special AS products_special, products.featured AS
> > products_featured, products.newproduct AS products_newproduct
> > FROM products
> > WHERE products.productid = %(param_1)s
> > 15:21:57,694 INFO  [sqlalchemy.engine.base.Engine.0x...d3d0]
> > {'param_1': None}
> > []
>
> > I had expected the bind variable param_1 to equal the fk of
> > 'DININGCHAIR'
>
> > What am I missing?
>
> > On Feb 26, 2:50 pm, Michael Bayer <[email protected]> wrote:
>
> > > 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-pr...
>
> > > > --
> > > > 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 
> > > > athttp://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