2

I'm trying to set a unique constraint between two columns (name and a), one of type text, and one of type json. How do I structure this in Postgres 9.6.3?

An example row looks like:

{ name: 'someName',
  a: {
    b: {
      c: 'some text'
    }
  }
}

What is the postgres command to create a table for this? Currently I have this, which gives a syntax error near (.

CREATE TABLE action ( id bigint NOT NULL, name text, a json, unique (name, payload::json#>>'{message, payload, content}') );

I've also tried

CREATE TABLE action (
    id bigint NOT NULL,
    name text,
    a json
);
ALTER TABLE actions ADD CONSTRAINT actions_constraint UNIQUE (payload::json#>>'{b, c, d}', name);

which keeps giving me this error:

ERROR:  syntax error at or near "::"

How can I create this constraint?

0

1 Answer 1

5

If you check documentation create table

and table_constraint is:

[CONSTRAINT constraint_name ]

UNIQUE ( column_name [, ... ] ) index_parameters |

UNIQUE in CREATE TABLE only accepts column names

So if you want index on an expression, you must add it after table created, command for that is CREATE INDEX (not accomplishable by alter table)

create unique INDEX actions_constraint on action (((a#>>'{b, c}')::text), name);  

CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ [ IF NOT EXISTS ] name ] ON table_name [ USING method ] ( { column_name | ( expression ) }

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, what actually worked for me is to remove the a from {a, b, c}. So the answer is create unique INDEX actions_constraint on action (((a#>>'{b, c}')::text), name); . Can you confirm this and change it so I can upvote your answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.