PostgreSQL check constraint for foreign key condition

前端 未结 4 1408
無奈伤痛
無奈伤痛 2020-12-13 10:45

I have a table of users eg:

create table \"user\" (
    id serial primary key,
    name text not null,
    superuser boolean not null default false
);
         


        
4条回答
  •  Happy的楠姐
    2020-12-13 11:08

    I don't know if this is a good way to do it but it seems to work

        INSERT INTO user_has_job (user_id, job_id) VALUES (you_user_id, your_job_id)
        WHERE EXIST (
            SELECT * FROM user WHERE id=your_user_id AND superuser=true
        );
    

提交回复
热议问题