You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2022/09/26 01:29:25 UTC

[GitHub] [airflow] uranusjr commented on a diff in pull request #26661: PostgresHook: Added ON CONFLICT DO NOTHING statement when all target fields are primary keys

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


##########
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’s slightly weird to me that `replace=True` (without setting `replace_index`) triggers `ON CONFLICT DO NOTHING` 🤔 



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org