I know that insert or update if key exists option for .to_sql() hasn't been implemented yet, so I'm looking for an alternative.
The first thing that comes to mind is to use the append option:
data.to_sql(
"Dim_Objects",
con=connection,
if_exists="append",
index=False
)
and remove duplicates in the database separately, after I inserted data:
DELETE FROM "Dim_Objects" a
USING "Dim_Objects" b
WHERE a."Code" = b."Code"
AND a."TimeStampUpdate" < b."TimeStampUpdate"
In this case, if there's a duplicate, I only keep the latest entry.
This approach seems to work but I hoped I could achieve the same using pandas directly.
Any ideas?