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.
On 17 Paź, 21:57, Michael Bayer <[EMAIL PROTECTED]> wrote:
> On Oct 17, 2008, at 3:32 PM, g00fy wrote:
>
>
>
> > hi,
> > on Mapper() i have relation() i use primaryjoin, and uselist=False,
> > but i also want to have LIMIT=1 on my join
> > it would be much faster!!
> > SA should add the limit itself when uselist=False
>
> uselist=False is intended for a one-to-one relation where there is
> only one child row referencing the parent row. It's not used to limit
> the size of a collection that is otherwise many elements.
>
> Since it seems you're wrestling with a large collection, your best
> option may be to use a "dynamic" loader which allows any query
> criterion to be used with an ordinary mapped attribute. This is
> described in:
> http://www.sqlalchemy.org/docs/05/mappers.html#advdatamapping_relatio...
>
> Another option would include setting up your primaryjoin to issue
> criterion which matches the exact row you're looking for. Here's an
> example:
>
> from sqlalchemy import *
> from sqlalchemy.orm import *
>
> engine = create_engine('sqlite://', echo=True)
> metadata =MetaData(engine)
> item = Table('item', metadata,
> Column('id', Integer, primary_key=True),
> Column('parent_id', Integer, ForeignKey('parent.id'))
> )
>
> parent = Table('parent', metadata,
> Column('id', Integer, primary_key=True),
>
> )
>
> metadata.create_all()
>
> engine.execute("insert into parent values(1)")
> engine.execute("insert into parent values(2)")
> engine.execute("insert into item values(1, 1)")
> engine.execute("insert into item values(2, 1)")
> engine.execute("insert into item values(3, 1)")
> engine.execute("insert into item values(4, 1)")
> engine.execute("insert into item values(5, 2)")
> engine.execute("insert into item values(6, 2)")
> engine.execute("insert into item values(7, 2)")
>
> class Parent(object):
> pass
>
> class Item(object):
> pass
>
> mapper(Parent, parent, properties={
> 'item':relation(Item, uselist=False,
>
> primaryjoin
> =
> item
> .c
> .id
> =
> =
> select
> ([func
> .max
> (item
> .c
> .id
> )]).where
> (item.c.parent_id==parent.c.id).correlate(parent).as_scalar(),
> viewonly=True
> )})
>
> mapper(Item, item)
>
> sess = create_session()
>
> p2 = sess.query(Parent).get(2)
> assert p2.item.id == 7
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---