Commit 254093e5 authored by ARCHER's avatar ARCHER 💬

do nothing if df has only pk on update

parent 68ae5585
......@@ -200,23 +200,31 @@ def to_sql(df,*args,**kwargs):
#tmp_table_orm = tmp_table_pd.table
logger.debug("merging tmp table")
columns_diff = set(tmp_table_orm.columns) ^ set(tmp_table_orm.columns)
if columns_diff:
raise ValueError("Missing column name in src or dst: %s" % str(columns_diff))
# exclude rows from tmp_table that are already in dest table
new_rows=except_all(tmp_table_orm.select(),table_orm.select())
# https://stackoverflow.com/questions/41724658/how-to-do-a-proper-upsert-using-sqlalchemy-on-postgresql
# get existing table orm
stmt = postgresql.insert(table_orm).from_select(tmp_table_orm.columns,new_rows)
on_conflict_stmt = stmt.on_conflict_do_update(
index_elements=table_orm.primary_key.columns,
set_={
k: getattr(stmt.excluded, k) for k in
[ c.name for c in table_orm.columns
non_pk_cols=[ c.name for c in table_orm.columns
if c not in list(table_orm.primary_key.columns)
]
}
)
if non_pk_cols:
on_conflict_stmt = stmt.on_conflict_do_update(
index_elements=table_orm.primary_key.columns,
set_={ k: getattr(stmt.excluded, k) for k in non_pk_cols }
)
else:
logger.debug("only primary key , so do nothing on update")
on_conflict_stmt = stmt.on_conflict_do_nothing()
try:
res=con.execute(on_conflict_stmt)
logger.info("insert/update %s rows" % res.rowcount)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment