diff --git a/src/ops/targets/postgres.rs b/src/ops/targets/postgres.rs index 65f59e15a..9b1766cb5 100644 --- a/src/ops/targets/postgres.rs +++ b/src/ops/targets/postgres.rs @@ -197,8 +197,7 @@ impl ExportContext { .map(|f| format!("\"{}\"", f.name)) .collect::>() .join(", "); - let value_fields = value_fields_schema - .iter() + let all_fields = (key_fields_schema.iter().chain(value_fields_schema.iter())) .map(|f| format!("\"{}\"", f.name)) .collect::>() .join(", "); @@ -211,15 +210,15 @@ impl ExportContext { Ok(Self { db_ref, db_pool, + upsert_sql_prefix: format!("INSERT INTO {table_name} ({all_fields}) VALUES "), + upsert_sql_suffix: if value_fields_schema.is_empty() { + format!(" ON CONFLICT ({key_fields}) DO NOTHING;") + } else { + format!(" ON CONFLICT ({key_fields}) DO UPDATE SET {set_value_fields};") + }, + delete_sql_prefix: format!("DELETE FROM {table_name} WHERE "), key_fields_schema, value_fields_schema, - upsert_sql_prefix: format!( - "INSERT INTO {table_name} ({key_fields}, {value_fields}) VALUES " - ), - upsert_sql_suffix: format!( - " ON CONFLICT ({key_fields}) DO UPDATE SET {set_value_fields};" - ), - delete_sql_prefix: format!("DELETE FROM {table_name} WHERE "), }) } }