Skip to content

Commit 3c18e57

Browse files
committed
assert check condition is single-column
1 parent fbb0bba commit 3c18e57

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/lib/PostgresMetaColumns.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ $$;
337337
DO $$
338338
DECLARE
339339
v_conname name;
340+
v_conkey int2[];
340341
BEGIN
341342
SELECT conname into v_conname FROM pg_constraint WHERE
342343
contype = 'c'
@@ -353,8 +354,18 @@ BEGIN
353354
END IF;
354355
355356
ALTER TABLE ${ident(old!.schema)}.${ident(old!.table)} ADD CONSTRAINT ${ident(
356-
old!.table
357-
)}_${ident(old!.name)}_check CHECK (${check});
357+
`${old!.table}_${old!.name}_check`
358+
)} CHECK (${check});
359+
360+
SELECT conkey into v_conkey FROM pg_constraint WHERE conname = ${literal(
361+
`${old!.table}_${old!.name}_check`
362+
)};
363+
364+
ASSERT v_conkey IS NOT NULL, 'error creating column constraint: check condition must refer to this column';
365+
ASSERT cardinality(v_conkey) = 1, 'error creating column constraint: check condition cannot refer to multiple columns';
366+
ASSERT v_conkey[1] = ${literal(
367+
old!.ordinal_position
368+
)}, 'error creating column constraint: check condition cannot refer to other columns';
358369
END
359370
$$;
360371
`

0 commit comments

Comments
 (0)