Given the following tables
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL,
organization_id TEXT NOT NULL,
UNIQUE (name, email)
);
CREATE TABLE tasks (
id SERIAL PRIMARY KEY,
users_id TEXT NOT NULL,
organization_id TEXT NOT NULL,
description TEXT NOT NULL
);
Is it possible to create constraints for 'user_id' column , that i can use values form 'users' table only with the same organization_id?
Best Answer
A check constraint that references another table like that is called a
FOREIGN KEY
. You can do what you want withMATCH FULL
As a special note the types must be the same, so in your example
tasks.users_id
is text. It must be int (the underlying type ofusers.id
).You can add the constriant after the fact like this..