Eric Ongerth wrote:
> If anyone out there has already implemented a custom DictOfLists
> collection class to map scalar keys to lists of values, I would be
> grateful for the opportunity to avoid reinventing the wheel.  If not,
> I guess I'll start working on it.
> 
> I've experimented successfully with attribute_mapped_collection and
> column_mapped_collection and they work just great.  However, instead
> of mapping keys to single values, I need some of my mapper relations
> to map to a list of values.
> 
> Here is more of the picture, for example.
> 
> foos = Table('foos', metadata,
>     Column('id', Integer, primary_key=True),
>     Column('name', String(20)))
> 
> bars = Table('bars', metadata,
>     Column('id', Integer, primary_key=True),
>     Column('foo_id', Integer, ForeignKey('foos.id')),
>     Column('value', String(20)),
>     Column('parent_id', Integer, ForeignKey('bars.id')))
> 
> class Foo(object): pass
> class Bar(object): pass
> 
> mapper(Foo, foos)
> 
> mapper(Bar, bars, properties={
>     'foo':relation(Foo, uselist=False, backref='bars'),
>     'children':relation(Bar,
>                 backref=backref('parent',
>                                         remote_side=[bars.c.id]))
> })
> 
> 
> ... So we have a relation of 1 Foo : many Bars.  And within the Bars
> we also have 'adjacency' (tree-like) relations between the various
> rows of the 'bars' table.  A Bar's children are kept in the standard
> list-like collection class.
> 
> But what I really need is a *dict* instead of a list.  Ok, SA already
> takes care of that.  But I actually need a list-like collection to
> appear as the value for each key in the dict.  Specifically, I need
> each Bar to be able to have stored children *per Foo*.  And not keyed
> by the parent's foo, but the child's foo.
> 
> Does that make sense?  I'll be working on this immediately, but if
> anyone can shorten my path to getting this straight I'd be very glad.
> I'm beginning to work out the use of a custom collection_class for
> this, but I haven't done all that much with metaclassing and the way
> forward isn't obvious (the SA instructions about this seem to assume
> the programmer is pretty experienced with custom subclassing etc.)

You've spurred me on to finish a "collections of collections" article 
I've been working on.  Part one is up and hopefully will shed some light 
on how these sorts of collections can be composed.

http://blog.discorporate.us/2008/02/sqlalchemy-partitioned-collections-1/

The whole collections toolkit is new in 0.4 and as you've discovered, 
doesn't have a large body of documentation yet.

Cheers,
Jason


--~--~---------~--~----~------------~-------~--~----~
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