Allen Bierbaum wrote:
> I have just started using column_mapped_collections.
> (http://www.sqlalchemy.org/docs/04/mappers.html#advdatamapping_relation_collections_dictcollections
> 
> I must say, these are very powerful and extremely nice when reading
> data.  But I have run into one thing that seems confusing when it
> comes to creating new objects in a session.  Namely, it is possible to
> add data to the mapped dictionary in such a way that the data
> structure is inconsistent and not what it would be when reading the
> same data back.
> 
> Using the example from the documentation as a start:
> 
> mapper(Item, items_table, properties={
>     'notes': relation(Note,
> collection_class=column_mapped_collection(notes_table.c.keyword)),
> })
> 
> # ...
> item = Item()
> item.notes['color'] = Note('color', 'blue')   # Set keyword attribute to 
> 'color'
> print item.notes['color']
> 
> Everything is good here, but what if I did it this way instead....
> 
> item.notes['not-color'] = Note('color', 'blue')
> 
> This last line is the problem because it has inserted a link to a new
> Note that has a keyword of
> 'color' but is showing up in the dictionary as 'not-color'.  If we
> flush all of this and reload from the database using a query, there
> will be no 'not-color' entry in the database.
> 
> Anyway, I think this is a bit non-intuitive.  What I propose instead
> is that SA could automatically set the 'keyword' attribute of the Note
> object as part of the process of assigning it to the mapped collection
> dictionary.  This way the insert could look more like:
> 
> item.notes['not-color'] = Note(value='blue')
> 
> and behind the scenes SA would call:  <new Note>.keyword = 'not-color'
> 
> Any thoughts on this?  Has anyone tried this in the past?

MappedCollection doesn't currently have a mismatch guard on __setitem__ 
(d[key] = val) or setdefault(), but easily could.  There *is* a guard 
protecting against item.notes = {'not-color': Note('color', 'blue')}, so 
that machinery is available and applying it to the other setters is 
straightforward.

Automatically setting the value for the attribute_ and column_mapped 
dict collections would be pretty convenient and DRY.  This is a great 
time to integrate that feature, if you want to try your hand at putting 
together a patch and tests.  If it's not too disruptive to existing 
users it could slide right in as a new feature of 0.5.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
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