我有一个类似于此的模式:
create table users (
id bigint primary key generated always as identity,
name text not null
);
create table feature (
id bigint primary key generated always as identity,
value text not null check (value != '')
);
create unique index ux1 on feature(value);
-- link table
create table user_feature (
id bigint generated always as identity,
user_id bigint not null references users(id),
feature_id bigint not null references feature(id)
);
create unique index ux2 on user_feature(user_id, feature_id);
-- feature values
insert into feature (value) values ('A'), ('B'), ('C'), ('D');
因此,用户可以有0个或多个功能,他可以只有一种类型的相同功能,但我想在用户可以拥有的功能之间添加一些逻辑/条件.
例如,假设功能B
和C
是不兼容的,因此用户可以没有它们,B
或C
,但不能两者都有,不能将B
和C
放在一起.
如果有的话,有什么适当的方法来做到这一点?
我正在阅读,如果我没有错,唯一的选项是触发器对吗(在插入/更新时)? 我不习惯触发器,如果有人能至少举个例子的话.
我使用的是版本14.
谢谢你的帮助.