4

Only one row is allowed to have parent_id NULL:

CREATE TABLE simple21_page (
    id integer NOT NULL,
    name character varying(120) NOT NULL,
    text text NOT NULL,
    parent_id integer
);

This is a tree and there should be exactly one root node.

I tried this, but it does not work:

create unique index on simple21_page (parent_id) where parent_id is null;

Is this possible with an constraint or unique index, or is a trigger needed?

2 Answers 2

7
+100

You are almost there. To get a singleton, you need a unique constraint on a constant value:

CREATE UNIQUE INDEX ON simple21_page ((1)) WHERE parent_id IS NULL;
Sign up to request clarification or add additional context in comments.

Comments

1

Another way to enforce this kind of uniqueness is usage of COALESCE and index on expression:

CREATE UNIQUE INDEX ON simple21_page(COALESCE(parent_id, -1));

The value provided as substitute should be out of scope of permitted values.

db<>fiddle demo

 INSERT INTO simple21_page VALUES (1, 'a', 'text', NULL);
 -- success

 INSERT INTO simple21_page VALUES (2, 'b', 'text', NULL);
 -- ERROR:  duplicate key value violates unique constraint "simple21_page_coalesce_idx"

Comments

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.