Ecto - 表 B 或 C 的表 A 上的外键约束,但不是 B 和 C

Rap*_*nah 2 sql postgresql elixir ecto

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

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

这种关系如何在 Elixir 的 Ecto 库中表现出来?

Gor*_*off 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)
)
Run Code Online (Sandbox Code Playgroud)