Many to Many

many_to_many association happens through a join schema or source, containing foreign keys to the associated schemas. For example, the association below:

# from MyApp.Post
many_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

  1. Create Tables

create table("todo_lists") do
  add :title
  timestamps()
end

create table("todo_items") do
  add :description
  timestamps()
end

create 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

No id recommended for join tables, basically because it creates the a useless "primary" index tree in db data structure

  1. Create Schemas

  1. Inserting Data

In this form

With these changesets

Last updated