Cri*_*cia 4 elixir ecto phoenix-framework
我有一个files表,还有许多其他表创建一对一的关联,例如users可能有一个avatar和posts可能有photo.
一种可能的解决方案是创建users_files和posts_files表格和使用has_one :through.但是,这看起来过分了.
理想的解决方案是定义这样的表
users
- avatar_id
posts
- photo_id
Run Code Online (Sandbox Code Playgroud)
并且有with:参数,has_one所以模式看起来像这样
schema "users" do
has_one :avatar, MyApp.FileDb, with: :avatar_id, foreign_key: :id #id is default
end
schema "posts" do
has_one :photo, MyApp.FileDb, with: :photo_id, foreign_key: :id
end
Run Code Online (Sandbox Code Playgroud)
这样你就不需要定义一个belongs_toon files.是否有类似的机制?在凤凰城处理这个问题的标准方法是什么?
| 归档时间: |
|
| 查看次数: |
3049 次 |
| 最近记录: |