我想知道在构建应用程序时为phoenix.gen.html
或phoenix.gen.json
指定了多少个关系。使用references
创建一对多关系很常见,如下所示:
mix phoenix.gen.model Video videos name:string approved_at:datetime description:text likes:integer views:integer user_id:references:users
但是如何传递多对多的字段?
答案 0 :(得分:2)
运行
mix phoenix.gen.model UserVideo users_videos user_id:references:users video_id:references:videos
然后更新您的架构
alias MyApp.{User, UserVideo}
schema "videos" do
...
many_to_many :users, User, join_through: UserVideo
end
alias MyApp.{Video, UserVideo}
schema "users" do
...
many_to_many :videos, Video, join_through: UserVideo
end