By default, SQLAlchemy only tracks changes to the value itself, which works as expected for simple values, such as integers and strings:
alice.name = "Alice" alice.age = 8
This also works when you assign a new value to a "complex type" column, such as dict or list:
alice.toys = ['doll', 'teddy bear']
However, SQLAlchemy does not notice the changes if you change one of the items in the list or add / remove a value:
alice.toys[0] = 'teapot' alice.toys.append('lego bricks')
For this to work, you can make sure that you assign a new list every time:
toys = alice.toys[:] # makes a "clone" of the existing list toys[0] = 'teapot' toys.append('lego bricks') alice.toys = toys
Or, read the Mutation Tracking chapter in the SQLAlchemy documentation to see how you can subclass a list or an indication so that they can track changes to their elements.
In addition, since you mentioned that you are using Postgres, Postgres has a special type of ARRAY that you can use instead of JSON if all you need is to store lists. However, what was said above about mutation tracking also applies to ARRAY columns.
source share