Allen Bierbaum | 16 May 21:48

Is inserting new data with column_mapped_collection inconsistent?


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
(Continue reading)

jason kirtland | 16 May 23:54

Re: Is inserting new data with column_mapped_collection inconsistent?


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
(Continue reading)

Allen Bierbaum | 17 May 15:56

Re: Is inserting new data with column_mapped_collection inconsistent?


On Fri, May 16, 2008 at 4:54 PM, jason kirtland <jek <at> discorporate.us> wrote:
[..]
>>
>> 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.

I would be more then happy to look into this (I already have), but I
think my skills aren't quite up to the challenge.  Could you point me
in the general direction?
(Continue reading)

jason kirtland | 19 May 20:04

Re: Is inserting new data with column_mapped_collection inconsistent?


Allen Bierbaum wrote:
> On Fri, May 16, 2008 at 4:54 PM, jason kirtland <jek <at> discorporate.us> wrote:
> [..]
>>> 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.
> 
> I would be more then happy to look into this (I already have), but I
> think my skills aren't quite up to the challenge.  Could you point me
> in the general direction?

(Continue reading)


Gmane