Recently, I ran into the same problem, and this was not obvious.
The objects themselves are not polluted, but their attributes. As far as I know, SQLAlchemy will only write changed attributes, not the entire object.
If you set the attribute using set_attribute and are different from the attribute's original data, SQLAlchemy detects that the object is dirty (TODO: I need details on how it does the comparison):
from sqlalchemy.orm.attributes import set_attribute set_attribute(obj, data_field_name, data)
If you want to mark an object dirty regardless of the initial value of the attribute, regardless of its change or not, use flag_modified :
from sqlalchemy.orm.attributes import flag_modified flag_modified(obj, data_field_name)
source share