On Mar 28, 2013, at 12:46 AM, Andrew Tribone <[email protected]> wrote:

> Hey guys,
> 
> I've been exploring how the `Mutation` class works and I was just wondering 
> if I could get an explanation of a couple of things. First of all, in the 
> documentation for Mutation Tracking there is an example of the 
> `JSONEncodedDict` using the `MutableDict`. As far as I can tell, this only 
> works when you mutate a key of that dictionary and if you mutate nested 
> structures these changes are ignored.

Well, if you nest a dictionary inside of it, and would like it to report on 
changes, that would also need to be a MutableDict instance.   The __setitem__() 
method of MutableDict would probably need to do a "coerce" at that point of the 
incoming object to a MutableDict (or MutableList), and then probably share the 
_parents collection of the base MutableDict with the child one being 
associated.   


> Upon further investigation the `Mutable` class has an property `_parents` 
> which I don't fully understand.  

this is a collection of weak references to objects which own a reference to the 
target mutable collection:


<parent object A >   --- .key --->  <mutable collection B>
                                                            
<mutable collection B> ---> ._parents  --->  {key: weakref(<parent object A>)}

> Can I get an explanation of what the desired use of the `Mutable` interface 
> is?

The collection upon which changes are to be tracked subclasses the Mutable 
mixin class.   The Mutable class, when configured in a mapping,  receives 
events denoting when an instance of the Mutable collection is associated with a 
parent object.   In response to this event, it appends the parent object to the 
local ._parents collection established upon the Mutable instance.   The Mutable 
instance, when a change is detected, calls upon the local .changed() method, 
which sends a change event to all parent objects represented in the ._parents 
collection, thus establishing a pending change for the ORM to forward onto the 
flush process.

the example and description in the docs should be illustrative here:

http://docs.sqlalchemy.org/en/rel_0_8/orm/extensions/mutable.html#establishing-mutability-on-scalar-column-values


> How exactly are mutations of children supposed to be propagated to its 
> parents?

the user-implemented collection class is responsible for intercepting these 
changes and calling upon the changed() method.   The MutableDict object 
provided only handles changes to its immediate list of key/values, not within 
those values.

> And is there an interface for adding parents to the `_parents` property (as 
> far as I can tell, no)?

the Mutable object itself is responsible for intercepting events denoting when 
the collection is associated with a parent and mutating the .parents 
collection.  If a single Mutable object wishes to refer to one or more 
sub-objects that also are Mutable, they would best take the approach of either 
propagating their change up to the parent Mutable object which then calls 
changed(), or by sharing the same ._parents collection among all objects in the 
structure.  


-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to