Skip to content

Commit 8514ff0

Browse files
committed
fix: duplicate columns on cols w/ multiple checks
1 parent 75da739 commit 8514ff0

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

Diff for: src/lib/sql/columns.sql

+3-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ FROM
8383
WHERE contype = 'u' AND cardinality(conkey) = 1
8484
) AS uniques ON uniques.table_id = c.oid AND uniques.ordinal_position = a.attnum
8585
LEFT JOIN (
86-
SELECT
86+
-- We only select the first column check
87+
SELECT DISTINCT ON (table_id, ordinal_position)
8788
conrelid AS table_id,
8889
conkey[1] AS ordinal_position,
8990
substring(
@@ -93,7 +94,7 @@ FROM
9394
) AS "definition"
9495
FROM pg_constraint
9596
WHERE contype = 'c' AND cardinality(conkey) = 1
96-
ORDER BY oid asc
97+
ORDER BY table_id, ordinal_position, oid asc
9798
) AS check_constraints ON check_constraints.table_id = c.oid AND check_constraints.ordinal_position = a.attnum
9899
WHERE
99100
NOT pg_is_other_temp_schema(nc.oid)

Diff for: test/lib/columns.ts

+33
Original file line numberDiff line numberDiff line change
@@ -899,3 +899,36 @@ create table public.t (
899899

900900
await pgMeta.query(`drop table public.t;`)
901901
})
902+
903+
test('column with multiple checks', async () => {
904+
await pgMeta.query(`create table t(c int8 check (c != 0) check (c != -1))`)
905+
906+
const res = await pgMeta.columns.list()
907+
const columns = res.data
908+
?.filter((c) => c.schema === 'public' && c.table === 't')
909+
.map(({ id, table_id, ...c }) => c)
910+
expect(columns).toMatchInlineSnapshot(`
911+
[
912+
{
913+
"check": "c <> 0",
914+
"comment": null,
915+
"data_type": "bigint",
916+
"default_value": null,
917+
"enums": [],
918+
"format": "int8",
919+
"identity_generation": null,
920+
"is_generated": false,
921+
"is_identity": false,
922+
"is_nullable": true,
923+
"is_unique": false,
924+
"is_updatable": true,
925+
"name": "c",
926+
"ordinal_position": 1,
927+
"schema": "public",
928+
"table": "t",
929+
},
930+
]
931+
`)
932+
933+
await pgMeta.query(`drop table t`)
934+
})

0 commit comments

Comments
 (0)