Last active
October 19, 2015 09:59
-
-
Save taybin/22f13ad1a5c90db23e6a to your computer and use it in GitHub Desktop.
ecto many to many association
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Foo.Model.Job do | |
alias Foo.Model.JobToJob, as: JobToJob | |
use Ecto.Model | |
schema "jobs" do | |
field :name, :string | |
has_many :children, JobToJob, foreign_key: :parent_id | |
has_many :parents, JobToJob, foreign_key: :child_id | |
end | |
end | |
defmodule Foo.Model.JobToJob do | |
alias Foo.Model.Job, as: Job | |
use Ecto.Model | |
schema "job_to_job", primary_key: false do | |
belongs_to :parent, Job, foreign_key: :parent_id | |
belongs_to :child, Job, foreign_key: :child_id | |
end | |
end | |
job1 = %Job{name: "parent"} |> Repo.insert | |
job2 = %Job{name: "child2"} |> Repo.insert | |
jobToJob = %JobToJob{parent: job1, child: job2} | |
jobToJob = Repo.insert(jobToJob) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment