diff --git a/Sources/Db/APIs/PostgreSQL.php b/Sources/Db/APIs/PostgreSQL.php index 5158559e64..e723a0385c 100644 --- a/Sources/Db/APIs/PostgreSQL.php +++ b/Sources/Db/APIs/PostgreSQL.php @@ -411,21 +411,13 @@ public function insert(string $method, string $table, array $columns, array $dat // PostgreSQL doesn't support replace: we implement a MySQL-compatible behavior instead if ($method == 'replace' || $method == 'ignore') { - $key_str = ''; + $key_str = implode(',', $keys); $col_str = ''; - $count = 0; - $count_pk = 0; + // Make a list of the non-pk fields. foreach ($columns as $columnName => $type) { - // Check pk field. - if (\in_array($columnName, $keys)) { - $key_str .= ($count_pk > 0 ? ',' : ''); - $key_str .= $columnName; - $count_pk++; - } - // Normal field. - elseif ($method == 'replace') { + if (!\in_array($columnName, $keys) && ($method == 'replace')) { $col_str .= ($count > 0 ? ',' : ''); $col_str .= $columnName . ' = EXCLUDED.' . $columnName; $count++; diff --git a/Sources/Db/Schema/Table.php b/Sources/Db/Schema/Table.php index c754e36cac..40f6590938 100644 --- a/Sources/Db/Schema/Table.php +++ b/Sources/Db/Schema/Table.php @@ -547,7 +547,7 @@ public function populate(bool $replace = false): int table: '{db_prefix}' . $this->name, columns: Db::$db->getTypeIndicators('{db_prefix}' . $this->name, reset($this->initial_data)), data: array_map(fn($row) => array_values($row), $this->initial_data), - keys: isset($auto_col) ? [$auto_col] : [], + keys: isset($auto_col) ? [$auto_col] : array_column($this->indexes['primary']->columns, 'name'), returnmode: $returnmode, );