Skip to content

Commit 3b6c4aa

Browse files
committed
fix: duplicated columns w/ multiple unique constraints
1 parent fab59a7 commit 3b6c4aa

File tree

2 files changed

+52
-23
lines changed

2 files changed

+52
-23
lines changed

src/lib/sql/columns.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ FROM
7676
) ON t.typtype = 'd'
7777
AND t.typbasetype = bt.oid
7878
LEFT JOIN (
79-
SELECT
79+
SELECT DISTINCT ON (table_id, ordinal_position)
8080
conrelid AS table_id,
8181
conkey[1] AS ordinal_position
8282
FROM pg_catalog.pg_constraint

test/lib/columns.ts

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,39 @@ test('column with multiple checks', async () => {
934934
await pgMeta.query(`drop table t`)
935935
})
936936

937+
test('column with multiple unique constraints', async () => {
938+
await pgMeta.query(`create table t(c int8 unique); alter table t add unique (c);`)
939+
940+
const res = await pgMeta.columns.list()
941+
const columns = res.data
942+
?.filter((c) => c.schema === 'public' && c.table === 't')
943+
.map(({ id, table_id, ...c }) => c)
944+
expect(columns).toMatchInlineSnapshot(`
945+
[
946+
{
947+
"check": null,
948+
"comment": null,
949+
"data_type": "bigint",
950+
"default_value": null,
951+
"enums": [],
952+
"format": "int8",
953+
"identity_generation": null,
954+
"is_generated": false,
955+
"is_identity": false,
956+
"is_nullable": true,
957+
"is_unique": true,
958+
"is_updatable": true,
959+
"name": "c",
960+
"ordinal_position": 1,
961+
"schema": "public",
962+
"table": "t",
963+
},
964+
]
965+
`)
966+
967+
await pgMeta.query(`drop table t`)
968+
})
969+
937970
test('dropping column checks', async () => {
938971
await pgMeta.query(`create table public.t(c int8 check (c != 0))`)
939972

@@ -955,34 +988,30 @@ test('column with fully-qualified type', async () => {
955988
schema: 'public',
956989
name: 't',
957990
})
958-
const column = await pgMeta.columns.create({
991+
const { data } = await pgMeta.columns.create({
959992
table_id: table.data!.id,
960993
name: 'c',
961994
type: 's.my_type',
962995
})
996+
const { id, table_id, ...column } = data!
963997
expect(column).toMatchInlineSnapshot(`
964998
{
965-
"data": {
966-
"check": null,
967-
"comment": null,
968-
"data_type": "USER-DEFINED",
969-
"default_value": null,
970-
"enums": [],
971-
"format": "my_type",
972-
"id": "16619.1",
973-
"identity_generation": null,
974-
"is_generated": false,
975-
"is_identity": false,
976-
"is_nullable": true,
977-
"is_unique": false,
978-
"is_updatable": true,
979-
"name": "c",
980-
"ordinal_position": 1,
981-
"schema": "public",
982-
"table": "t",
983-
"table_id": 16619,
984-
},
985-
"error": null,
999+
"check": null,
1000+
"comment": null,
1001+
"data_type": "USER-DEFINED",
1002+
"default_value": null,
1003+
"enums": [],
1004+
"format": "my_type",
1005+
"identity_generation": null,
1006+
"is_generated": false,
1007+
"is_identity": false,
1008+
"is_nullable": true,
1009+
"is_unique": false,
1010+
"is_updatable": true,
1011+
"name": "c",
1012+
"ordinal_position": 1,
1013+
"schema": "public",
1014+
"table": "t",
9861015
}
9871016
`)
9881017

0 commit comments

Comments
 (0)