Re: jsonb unique constraints - Mailing list pgsql-general

From David G. Johnston
Subject Re: jsonb unique constraints
Date
Msg-id CAKFQuwZU_JYNV6MrwZfi6=RqEHkwKtx6MPb2nqVSr-_22QMSjQ@mail.gmail.com
Whole thread Raw
In response to jsonb unique constraints  (Ted Toth <txtoth@gmail.com>)
Responses Re: jsonb unique constraints  (Ted Toth <txtoth@gmail.com>)
List pgsql-general
On Tue, May 5, 2020 at 8:33 AM Ted Toth <txtoth@gmail.com> wrote:
Can you have unique constraints on jsonb columns keys? I've looked for examples but haven't found any what is the proper syntax? Here's what I tried:

CREATE TABLE report_json (
    recnum int,
    id integer,
    report jsonb,
    PRIMARY KEY (recnum),
    CONSTRAINT report_json_unique_constraint UNIQUE (id,((report ->> 'data')::int),((report ->> 'ctc')::int),((report ->> 'dtg')::int)
);

which causes a syntax error at the first '(' around 'report ->>'.

Documentation says constraints must reference column names - so no.  You can probably get what you are after by directly creating a unique index though - those allow expressions.

David J.

pgsql-general by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: Installing Postgis25_11
Next
From: Clifford Snow
Date:
Subject: Re: Installing Postgis25_11