I have a table t
, with a column called json
, of type JSON
. Within the JSON there is a natural key:
> SELECT json->'id' AS id FROM t LIMIT 1;
id
-----------------------------
" 63631ff3809de7a17398602f"
I can create a UNIQUE INDEX
on id
, thus:
> CREATE UNIQUE INDEX t_id ON t((json->>'id'));
CREATE INDEX
I'd like to add this as a table_constraint_using_index, but it fails for both PRIMARY KEY
:
> ALTER TABLE t ADD CONSTRAINT t_pkey PRIMARY KEY USING INDEX t_id;
ERROR: index "t_id" contains expressions
LINE 1: ALTER TABLE t ADD CONSTRAINT t_pkey
^
DETAIL: Cannot create a primary key or unique constraint using such an index.
and UNIQUE
:
> ALTER TABLE t ADD CONSTRAINT t_unique_id UNIQUE USING INDEX t_id;
ERROR: index "t_id" contains expressions
LINE 1: ALTER TABLE t ADD CONSTRAINT t_unique_id...
^
DETAIL: Cannot create a primary key or unique constraint using such an index.
Should I be able to add such a constraint?