alexandermalyga commented on code in PR #26661:
URL: https://github.com/apache/airflow/pull/26661#discussion_r980335960


##########
airflow/providers/postgres/hooks/postgres.py:
##########
@@ -270,10 +270,14 @@ def _generate_insert_sql(
                 raise ValueError("PostgreSQL ON CONFLICT upsert syntax 
requires an unique index")
             if isinstance(replace_index, str):
                 replace_index = [replace_index]
-            replace_index_set = set(replace_index)
 
-            replace_target = [
-                "{0} = excluded.{0}".format(col) for col in target_fields if 
col not in replace_index_set
-            ]
-            sql += f" ON CONFLICT ({', '.join(replace_index)}) DO UPDATE SET 
{', '.join(replace_target)}"
+            on_conflict_str = f" ON CONFLICT ({', '.join(replace_index)})"
+            replace_target = [f for f in target_fields if f not in 
replace_index]
+
+            if replace_target:
+                replace_target_str = ", ".join(f"{col} = excluded.{col}" for 
col in replace_target)
+                sql += f"{on_conflict_str} DO UPDATE SET {replace_target_str}"
+            else:
+                sql += f"{on_conflict_str} DO NOTHING"

Review Comment:
   It will not trigger `ON CONFLICT DO NOTHING` because the check is done 
against `replace_target` and not `replace_index`.
   But now that I think about it, if `replace_index=()`, the generated SQL will 
not be valid: `ON CONFLICT ()`. To avoid that we should change `if 
replace_index is None:` to `if not replace_index:` before raising the exception.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to