脚手架app时有很多关系

时间:2016-09-14 09:56:14

标签: phoenix-framework ecto

我想知道在构建应用程序时为phoenix.gen.htmlphoenix.gen.json指定了多少个关系。使用references创建一对多关系很常见,如下所示:

mix phoenix.gen.model Video videos name:string approved_at:datetime description:text likes:integer views:integer user_id:references:users

但是如何传递多对多的字段?

1 个答案:

答案 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