Thanks Michael, thats just too cool!
On Jul 20, 10:35 pm, Michael Bayer <[EMAIL PROTECTED]> wrote:
> yup, just spell it out
>
> item_mapper = mapper(Item, item_table, properties=dict(
> collections=relation(Item, secondary=collection_table ,lazy=True,
> primaryjoin=item_table.c.id==collection_table.c.id_coll,
> secondaryjoin=item_table.c.id==collection_table.c.id_item,
> backref='items')))
>
> ...
>
> # do a more reasonable collection setup than the original test
>
> (koen, dirk) = session.query(Item).order_by(desc
> (item_table.c.name)).all()
>
> koen.collections.append(dirk)
> assert dirk in koen.collections
> assert koen in dirk.items
> session.flush()
> session.clear()
>
> (koen, dirk) = session.query(Item).order_by(desc
> (item_table.c.name)).all()
> assert dirk in koen.collections
> assert koen in dirk.items
>
> On Jul 20, 2007, at 4:00 PM, Koen Bok wrote:
>
> > from sqlalchemy import *
>
> > metadata = BoundMetaData('sqlite://')
>
> > class Item(object):
> > def __init__(self, name):
> > self.name = name
> > def __repr__(self):
> > return '<%s:%s>' % (self.__class__.__name__, self.name)
>
> > item_table = Table('item', metadata,
> > Column('id', Integer, primary_key=True),
> > Column('name', String(50)))
>
> > collection_table = Table('collection', metadata,
> > Column('id_coll', Integer, ForeignKey("item.id"), nullable=False),
> > Column('id_item', Integer, ForeignKey("item.id"), nullable=False))
>
> > item_mapper = mapper(Item, item_table, properties=dict(
> > collections=relation(Item, secondary=collection_table ,lazy=True,
> > backref='items')))
>
> > metadata.create_all()
>
> > session = create_session()
>
> > session.save(Item('Koen Bok'))
> > session.save(Item('Dirk Stoop'))
>
> > session.flush()
>
> > # And now we should be able to do something like this:
>
> > items = session.query(Item).select()
>
> > for item in items:
> > for i in items:
> > item.items.append(i)
> > item.collections.append(i)
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---