'SQLAlchemy session commit does not update database (fastapi)

Trying to update a JSONB column in my database. I can see that the new column values are being passed into my method however upon commit and refresh, the changes do not appear.

node = session.query(Table).filter(Table.id==1).one()
stored_attributes = node.system_meta.get("attributes", [])
LOG.info(f"existing: {stored_attributes}")
stored_attributes.append(new_filename)
LOG.info(f"new list: {stored_attributes}")

node.update_system_values({"attributes": stored_attributes})
try:
    session.add(node)
    session.commit()
except Exception as e:
    session.rollback()
    LOG.error(e)

session.refresh(node)
LOG.info(f"refreshed: {node.system_meta}")

The update method on the JSONB column is bound to an ORM and is simply a dictionary update:

def update_system_values(self, new_values: Dict[str, Any]):
    system_meta = dict(self.system_meta)
    system_meta.update(new_values)
    self.system_meta = system_meta
    LOG.info(f"updated values: {system_meta}")

This is the FastAPI output:

[info] existing: ['floor-4-20220516-114137.png'] 
[info] new list: ['floor-4-20220516-114137.png', 'floor-4-20220516-144651.png'] 
[info] updated values: {'attributes': ['floor-4-20220516-114137.png', 'floor-4-20220516-144651.png'], 'floor_number': 4}
[info] refreshed: {'attributes': ['floor-4-20220516-114137.png'], 'floor_number': 4} 

Why aren't the values being persisted into the db?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source