【发布时间】:2021-06-10 15:40:46
【问题描述】:
如果我有profile、item、location 表:
-
profile有很多location。 -
item有一个location。 -
profile和item中的location不会重叠。
我应该如何设计location 表?
以下有效吗? location 属于 2 个表?
schema "profiles" do
...
has_many :location, Location
end
schema "items" do
...
has_one :location, Location
end
schema "locations" do
...
belongs_to :profile, Profile
belongs_to :item, Item
end
还是应该有 2 个location 表?
【问题讨论】:
-
我发现首先考虑tables要容易得多。代码中的抽象(如 Ecto 或 ORM)仅在它们使您与数据交互的工作变得更容易时才有用。我不确定我是否遵循您所说的“
profile和item中的location不会重叠”,但我没有遵循配置文件和项目之间的关系。定义架构中的所有关系不会获得额外积分:我建议从您需要的查询开始,例如“一个位置所有项目的列表”,并建立实现它们所需的关系。
标签: elixir phoenix-framework ecto