sqlalchemy insert on duplicate update
from sqlalchemy.dialects.mysql import insert insert_stmt = insert(my_table).values( id='some_existing_id', data='inserted value') on_duplicate_key_stmt = insert_stmt.on_duplicate_key_update( data=insert_stmt.inserted.data, status='U' ) conn.execute(on_duplicate_key_stmt)
# another example
data = {"task_id": task_id,
"task": task,
"execution_time": execution_time,
"start_dt": start_dt,
"end_dt": end_dt,
"state": state}
insert_stmt = mysql.insert(TaskInstance).values(**data)
on_duplicate_key_stmt = insert_stmt.on_duplicate_key_update(
{"start_dt": start_dt, "end_dt": end_dt})
session.execute(on_duplicate_key_stmt)