I want to insert multiple items into a table and upsert the table on conflict. This is what I came up with the following
from sqlalchemy.dialects.postgresql import insert
meta = MetaData()
jobs_table = Table('jobs', meta, autoload=True, autoload_with=engine)
stmt = insert(jobs_table).values(jobs)
stmt.on_conflict_do_update(
index_elements=['j_id'],
set_= dict(active=True)
)
result = engine.execute(stmt)
return result.is_insert
The j_id
is a unique field and I am trying to update the row if it already exists. I get the following error if the row already exists.
(psycopg2.IntegrityError) duplicate key value violates unique constraint "j_id"
DETAIL: Key (j_id)=(0ea445da-bd1d-5571-9906-0694fa85728a) already exists.
Is there something that I am missing here ?