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. Below I have an example of this.

    Base = declarative_base()
    engine = create_engine('sqlite:///:memory:', echo=True)

    class Model(Base):

        __tablename__ = 'model'

        id = Column(Integer, primary_key=True)
        blob = Column(MutableDict.as_mutable(JSONEncodedDict), 
nullable=False)

    Base.metadata.create_all(engine)

    model = Model(blob={'foo': 'bar'})

    session = sessionmaker(bind=engine)()
    session.add(model)
    session.commit()

    model.blob['foo'] = {'bar': 'foobar'}
    session.commit()

    session = sessionmaker(bind=engine)()
    model = session.query(Model).one()
    assert model.blob == {'foo': {'bar': 'foobar'}}
    model.blob['foo']['bar'] = {'foobar': 'barfoo'}
    session.commit()

    session = sessionmaker(bind=engine)()
    model = session.query(Model).one()
    print model.blob
    assert model.blob == {'foo': {'bar': {'foobar': 'barfoo'}}}

I create new sessions each time just to make sure the session isn't caching 
anything behind the scenes. The output of this looks like:

2013-03-27 21:34:58,825 INFO sqlalchemy.engine.base.Engine PRAGMA 
table_info("model")
2013-03-27 21:34:58,825 INFO sqlalchemy.engine.base.Engine ()
2013-03-27 21:34:58,826 INFO sqlalchemy.engine.base.Engine
CREATE TABLE model (
id INTEGER NOT NULL,
blob VARCHAR NOT NULL,
PRIMARY KEY (id)
)


2013-03-27 21:34:58,826 INFO sqlalchemy.engine.base.Engine ()
2013-03-27 21:34:58,826 INFO sqlalchemy.engine.base.Engine COMMIT
2013-03-27 21:34:58,827 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2013-03-27 21:34:58,828 INFO sqlalchemy.engine.base.Engine INSERT INTO 
model (blob) VALUES (?)
2013-03-27 21:34:58,828 INFO sqlalchemy.engine.base.Engine ('{"foo": 
"bar"}',)
2013-03-27 21:34:58,829 INFO sqlalchemy.engine.base.Engine COMMIT
2013-03-27 21:34:58,829 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2013-03-27 21:34:58,830 INFO sqlalchemy.engine.base.Engine SELECT model.id 
AS model_id, model.blob AS model_blob
FROM model
WHERE model.id = ?
2013-03-27 21:34:58,830 INFO sqlalchemy.engine.base.Engine (1,)
2013-03-27 21:34:58,831 INFO sqlalchemy.engine.base.Engine UPDATE model SET 
blob=? WHERE model.id = ?
2013-03-27 21:34:58,831 INFO sqlalchemy.engine.base.Engine ('{"foo": 
{"bar": "foobar"}}', 1)
2013-03-27 21:34:58,831 INFO sqlalchemy.engine.base.Engine COMMIT
2013-03-27 21:34:58,831 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2013-03-27 21:34:58,832 INFO sqlalchemy.engine.base.Engine SELECT model.id 
AS model_id, model.blob AS model_blob
FROM model
2013-03-27 21:34:58,832 INFO sqlalchemy.engine.base.Engine ()
2013-03-27 21:34:58,832 INFO sqlalchemy.engine.base.Engine COMMIT
2013-03-27 21:34:58,833 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2013-03-27 21:34:58,833 INFO sqlalchemy.engine.base.Engine SELECT model.id 
AS model_id, model.blob AS model_blob
FROM model
2013-03-27 21:34:58,833 INFO sqlalchemy.engine.base.Engine ()
{u'foo': {u'bar': u'foobar'}}
Traceback (most recent call last):
  File "mutability_tracking.py", line 279, in <module>
    assert model.blob == {'foo': {'bar': {'foobar': 'barfoo'}}}
AssertionError

Upon further investigation the `Mutable` class has an property `_parents` 
which I don't fully understand.  Can I get an explanation of what the 
desired use of the `Mutable` interface is? How exactly are mutations of 
children supposed to be propagated to its parents? And is there an 
interface for adding parents to the `_parents` property (as far as I can 
tell, no)?

I have a solution that works, but I'm not convinced it is the right way to 
do it, so I'll wait to see what people have to say about this before I 
share.

Thanks!!!

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
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