Skip to content

Commit bcf9ab1

Browse files
committed
support postgres identity columns
1 parent 46dd4b4 commit bcf9ab1

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/Platforms/PostgreSqlPlatform.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,14 @@ public function getColumns(string $table, ?string $schema = null): array
8888
CASE WHEN a.atttypmod = -1 THEN NULL ELSE a.atttypmod -4 END AS size,
8989
pg_catalog.pg_get_expr(ad.adbin, 'pg_catalog.pg_attrdef'::regclass)::varchar AS default,
9090
COALESCE(co.contype = 'p', FALSE) AS is_primary,
91-
COALESCE(co.contype = 'p' AND strpos(pg_get_expr(ad.adbin, ad.adrelid), 'nextval') = 1, FALSE) AS is_autoincrement,
91+
COALESCE(co.contype = 'p' AND (strpos(pg_get_expr(ad.adbin, ad.adrelid), 'nextval') = 1 OR a.attidentity != ''), FALSE) AS is_autoincrement,
9292
NOT (a.attnotnull OR t.typtype = 'd' AND t.typnotnull) AS is_nullable,
93-
SUBSTRING(pg_catalog.pg_get_expr(ad.adbin, 'pg_catalog.pg_attrdef'::regclass) FROM %s) AS sequence
93+
") . (
94+
count($tableArgs) > 1
95+
? "pg_get_serial_sequence('%table.%table', a.attname) AS sequence"
96+
: "pg_get_serial_sequence('%table', a.attname) AS sequence"
97+
)
98+
. (/** @lang GenericSQL */ "
9499
FROM
95100
pg_catalog.pg_attribute AS a
96101
JOIN pg_catalog.pg_class AS c ON a.attrelid = c.oid
@@ -108,7 +113,7 @@ public function getColumns(string $table, ?string $schema = null): array
108113
AND NOT a.attisdropped
109114
ORDER BY
110115
a.attnum
111-
"), "nextval[(]'\"?([^'\"]+)", ...$tableArgs);
116+
"), ...$tableArgs, ...$tableArgs);
112117

113118
$columns = [];
114119
foreach ($result as $row) {

tests/cases/integration/platform.postgres.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class PlatformPostgresTest extends IntegrationTestCase
5050
'isAutoincrement' => true,
5151
'isUnsigned' => false,
5252
'isNullable' => false,
53-
'meta' => ['sequence' => 'books_id_seq'],
53+
'meta' => ['sequence' => 'public.books_id_seq'],
5454
],
5555
'author_id' => [
5656
'name' => 'author_id',
@@ -122,7 +122,7 @@ class PlatformPostgresTest extends IntegrationTestCase
122122
'isAutoincrement' => true,
123123
'isUnsigned' => false,
124124
'isNullable' => false,
125-
'meta' => ['sequence' => 'eans_id_seq'],
125+
'meta' => ['sequence' => 'public.eans_id_seq'],
126126
],
127127
'code' => [
128128
'name' => 'code',
@@ -245,7 +245,7 @@ class PlatformPostgresTest extends IntegrationTestCase
245245

246246
public function testPrimarySequence()
247247
{
248-
Assert::same('books_id_seq', $this->connection->getPlatform()->getPrimarySequenceName('books'));
248+
Assert::same('public.books_id_seq', $this->connection->getPlatform()->getPrimarySequenceName('books'));
249249
}
250250

251251

0 commit comments

Comments
 (0)