many_to_manyassociation happens through a join schema or source, containing foreign keys to the associated schemas. For example, the association below:
# from MyApp.Postmany_to_many :tags,MyApp.Tag, join_through: "posts_tags"#is backed by relational databases through a join table as follows:[Post] <-> [posts_tags] <-> [Tag] id <-- post_id tag_id --> id
Step by Step Guide
Create Tables
create table("todo_lists") do add :titletimestamps()endcreate table("todo_items") do add :descriptiontimestamps()endcreate table("todo_list_x_items", primary_key: false) do add :todo_item_id,references(:todo_items) add :todo_list_id,references(:todo_lists)#timestamps()end
# In MyApp.TodoListdefchangeset(struct, params \\ %{}) do struct|>Ecto.Changeset.cast(params, [:title])|>Ecto.Changeset.cast_assoc(:todo_items, required: true)end# And then in MyApp.TodoItemdefchangeset(struct, params \\ %{}) do struct|>Ecto.Changeset.cast(params, [:description])end