Thanks!

I understand what was happening.  I had tried MutableDict, but I think 
my data was too complex (a list-valued dictionary) for the basic 
implementation.  When it didn't work I I got confused and thought 
reassigning the value would trigger mutation tracking.

On Tuesday, June 26, 2018 at 1:01:06 PM UTC-7, Mike Bayer wrote:
>
> On Tue, Jun 26, 2018 at 3:56 PM, Victor Reichert 
> <victor....@apptactoe.com <javascript:>> wrote: 
> > Hi, 
> > 
> > I have a class like: 
> > 
> > class SQLClass(db): 
> > 
> >   json_field = Column(sqlalchemy.dialects.postgresql.JSONB) 
> > 
> > I'm doing something like: 
> > 
> > session.add( 
> >      SQLClass(json_field = dict( 
> >         key = [value1] 
> >      ) 
> > )) 
> > 
> > session.commit() 
> > 
> > slq_class_obj = session.query(SQLClass).first() 
> > 
> > new_dict = slq_class_obj.json_field 
> > 
> > new_dict['key'].append(value2) 
> > 
> > slq_class_obj. json_field = new_dict 
> > 
> > print(slq_class_obj. json_field) # looks OK 
> > 
> > session.commt() 
> > 
> > print(slq_class_obj. json_field) # I just get [value1] 
> > 
> > The changes I made to json field (new_dict) were not persisted. 
>  However, if 
> > just change the json_field to be an empty dict that is persisted. 
> > 
> > I know there are some issues with mutation and the JSON data type, but I 
> > thought since I was setting the field value rather than just mutating 
> the 
> > dict in place the mutation would be tracked. 
>
> you are not creating a new dictionary above, just assigning the 
> attribute to itself.   that's just in-place mutation.   the attribute 
> system might log this as "changed" but when it goes to compare what it 
> loaded from the database with what the value is now, it will see they 
> are equal since they are the same object, and no net change. 
>
> if your jsonb is storing just a dictionary, place mutabledict around 
> it and your problem is solved. 
>
> http://docs.sqlalchemy.org/en/latest/orm/extensions/mutable.html?highlight=mutabledict#sqlalchemy.ext.mutable.MutableDict
>  
>
>
>
>
>
> > 
> > Why is setting the field to an empty dict persisted while my change is 
> not? 
> > Any general advice on handling JSON and mutation would also be welcome. 
> > 
> > Thank you for your help! 
> > 
> > ~Victor 
> > 
> > -- 
> > SQLAlchemy - 
> > The Python SQL Toolkit and Object Relational Mapper 
> > 
> > http://www.sqlalchemy.org/ 
> > 
> > To post example code, please provide an MCVE: Minimal, Complete, and 
> > Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> > description. 
> > --- 
> > 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+...@googlegroups.com <javascript:>. 
> > To post to this group, send email to sqlal...@googlegroups.com 
> <javascript:>. 
> > Visit this group at https://groups.google.com/group/sqlalchemy. 
> > For more options, visit https://groups.google.com/d/optout. 
>

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to