pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

Varchar `In` Check Constraints cause invalid sql generation

Open PadenZach opened this issue 10 months ago • 1 comments

Seems like there's an issue when using pgschema with varchars.

Given

-- example.sql
-- This works
CREATE TABLE if not exists example_working(
    id bigint generated by default as identity primary key,
    kind text NOT NULL,
    CONSTRAINT chk_valid_kind
        CHECK (kind IN ('foo','bar'))
);

-- This does not work
-- 
CREATE TABLE if not exists example_broken1(
    id bigint generated by default as identity primary key,
    kind varchar(3) NOT NULL,
    CONSTRAINT chk_valid_kind
        CHECK (kind IN ('foo','bar'))
);


`pgschema plan --file example.sql` generates:
```sql
-- Broken example generates...
CREATE TABLE IF NOT EXISTS example_broken1 (
    id bigint GENERATED BY DEFAULT AS IDENTITY,
    kind varchar(3) NOT NULL,
    CONSTRAINT example_pkey PRIMARY KEY (id),
    CONSTRAINT chk_valid_kind CHECK (kind::text IN ('foo'::character varying, 'bar'::character varying]::text[))
);

this fails due to invalid sql:

ERROR:  syntax error at or near "]"
LINE 5: ...'foo'::character varying, 'bar'::character varying]::text[))

pgschema version: 1.6.1 postgres version: 17.6

PadenZach avatar Jan 30 '26 00:01 PadenZach

We currently work around this by moving to entirely text types instead of varchar, which I prefer anyways due to some locking edge cases.

PadenZach avatar Jan 30 '26 00:01 PadenZach