Hi all, Thank you Jason, that's a great writeup, and the thread gives me some hints on what to do with my own project:
I'm trying to create a generic super class that knows how to automatically persist attributes of derived classes into a database. This would allow a user of the class to define basic data types, and not have to mappers or define the table or mapper. I've already written something that will automatically create columns for the simple data types like int and str but have encountered complications with lists and dicts. I'm currently using the pattern provided for using orderinglist ,http://www.sqlalchemy.org/docs/04/sqlalchemy_ext_orderinglist.html . But I'm not thrilled with my solution since the property name tends to collide with my class attribute name and I don't like having to make the user aware of the container class. It seems like the solution is going to involve writing a CollectionAdapter. Thank you, Judah De Paula [EMAIL PROTECTED] jason kirtland wrote: > 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 -~----------~----~----~----~------~----~------~--~---
