1

I would like to know if it's possible to create table with a not null constraint on a column depending of the value of another column in another table. (In postgres)

exemple:

CREATE TABLE IF NOT EXISTS Ref (
    id SERIAL PRIMARY KEY,
    required BOOLEAN DEFAULT FALSE
);

CREATE TABLE IF NOT EXISTS Value (
    id SERIAL PRIMARY KEY,
    refId INTEGER REFERENCES Ref(id) NOT NULL,
    value TEXT,
);

So here I would like to add a constraint NOT NULL to value if required is TRUE.

So I tried to use some check but it seem that it only check column inside the same table :(.

Thanks in advance.

1
  • You can (a) migrate the required column to the Value table, or (b) duplicate it in both tables and then automatically duplicate the values by trigger functions, or by rule, or (c) implement the constraint in trigger functions applying to table Value while testing the required column in table Ref Commented Nov 25, 2021 at 14:17

1 Answer 1

1

I understood that when required field is equal to true, then we must set NOT NULL to the Value table's field, else set to must be NULL. You can do this using PostgreSQL check constraints. Firstly we must create a function for checking field values.

CREATE OR REPLACE FUNCTION get_required(p_id integer, p_value text)
RETURNS BOOLEAN AS
$$
declare 
    v_ret bool;
begin
    select required into v_ret from tbl_ref where id = p_id;

    if (v_ret)and(p_value is null) then 
        return false; 
    end if;

    return true; 
END;
$$ LANGUAGE PLpgSQL;

Then we can use this function on the create table process:

CREATE TABLE tbl_val (
    id serial4 NOT NULL,
    ref_id int4 NOT NULL,
    "value" text NULL,
    CONSTRAINT tbl_val_check CHECK (get_required(ref_id, "value")),
    CONSTRAINT tbl_val_pkey PRIMARY KEY (id),
    CONSTRAINT tbl_val_fk FOREIGN KEY (ref_id) REFERENCES tbl_ref(id)
);
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot ! the function get_required() you created was just what I needed :)

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.