2

假设table A可以属于table Btable C,但不能同时属于两者。

似乎可以在 SQL Server 中为这种情况使用外键约束。

Elixir 的 Ecto 库如何表示这种关系?

4

1 回答 1

6

您可以将其构造为对每个外键约束使用单独的列,然后保证只填充一个:

create table A (
    . . . 
    b_id int,
    c_id int,
    foreign key (b_id) references b(b_id),
    foreign key (c_id) references c(c_id),
    check (b_id is null or c_id is null)
)
于 2020-10-30T00:09:28.423 回答