On Oct 17, 2008, at 4:22 PM, g00fy wrote:
>
> I can't make this work with my relation:
>
>
> mapper(Warehouse, warehouse_table,properties = {
> 'translation': relation(
> WarehouseTranslation,
> lazy = False,
> uselist = False,
> primaryjoin=
> and_(
> warehouse_table.c.id ==
> warehouse_translation_table.c.warehouse_id,
>
> warehouse_translation_table.c.language_id==common.get_language(),
> # I want limit 1 here
> ),
> ),
> .... rest relations
> an aditional subselect will just make it work slower.
can you see how placing a LIMIT=1 there, especially if you're using
eager loading, will totally break even the most basic operation ?
Such as session.query(Warehouse).all() - you'll only get the first
Warehouse object. The LIMIT idea, without a subquery, can only work
at all in the general case if you're using lazy loading (and
relation() should be configured to work in the genral case). If you
want one query that loads Warehouse objects each with just the first
matching WarehouseTranslation, you have to use a subquery in any case,
even if LIMIT is used. That's just plain SQL.
However, if you really want to load exactly one Warehouse and exactly
one WarehouseTranslation, this is not SQLA default behavior and is
very unique; but its allowed, you just need to be explicit and
construct a query which states that exactly. The example I gave would
use a query like:
p2 = sess.query(Parent).join(Parent.items).\
options(contains_eager(Parent.items))[0]
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---