using list on postgresql JSON type with sqlalchemy
You could flag an instance as modified manually
from sqlalchemy.orm.attributes import flag_modified
def view(session):
mylst = Session.query(MyList).get(1)
mylst.lst.append('45')
flag_modified(mylst, 'lst') # flag its `lst' attribute is modified
print(Session.is_active, Session.is_modified(mylst))
# (True, True)
By default, SQLAlchemy only tracks changes of the value itself, which works "as expected" for simple values, such as ints and strings:
alice.name = "Alice"
alice.age = 8
It also works when you assign a new value to a column of a "complex type", such as dict or list:
alice.toys = ['doll', 'teddy bear']
However, SQLAlchemy does not notice a change if you modify one of the elements in the list, or append/remove a value:
alice.toys[0] = 'teapot'
alice.toys.append('lego bricks')
To make this work you can either make sure you assign a new list each time:
toys = alice.toys[:] # makes a "clone" of the existing list
toys[0] = 'teapot'
toys.append('lego bricks')
alice.toys = toys
Or have a read of the Mutation Tracking chapter in SQLAlchemy docs to see how you can subclass a list or a dict so they track modifications of their elements.
Also, since you mentioned you're using Postgres - there's a dedicated ARRAY
type in Postgres which you can use instead of JSON
if all you need is to store lists. However, what is said above about the mutation tracking applies to columns of ARRAY
type too.