"""
This is comment which describes why it's required to manually flag the modified context to make sure it gets updated in the database.
According to the [SQLAlchemy documentation](https://docs.sqlalchemy.org/en/20/core/type_basics.html#sqlalchemy.types.JSON):
The JSON type, when used with the SQLAlchemy ORM, does not detect in-place mutations to the structure.
In order to detect these, the sqlalchemy.ext.mutable extension must be used, most typically using the MutableDict class.
This extension will allow “in-place” changes to the datastructure to produce events which will be detected by the unit of work.
Alternatively, assigning a JSON structure to an ORM element that replaces the old one will always trigger a change event.
[flag_modified](https://docs.sqlalchemy.org/en/20/orm/session_api.html#sqlalchemy.orm.attributes.flag_modified)
[mutable](https://docs.sqlalchemy.org/en/20/orm/extensions/mutable.html)
"""
with SessionLocal.begin() as db:
task = query_task(self.task_id, db)
task.task_uuid = task_id
task.status = TaskStatusEnum.FAILED
context = {
"error": str(exc),
"args": args,
"kwargs": kwargs,
"traceback": einfo.traceback,
}
if not task.context:
task.context = context
else:
task.context.update(context)
flag_modified(task, "context")
db.add(task)
Created
October 17, 2025 05:32
-
-
Save Harold2017/ecbd4758fba3752b605c428072a0d7be to your computer and use it in GitHub Desktop.
how to correctly update json column with SQLAlchemy
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment