Perhaps I should open a fresh post on this matter - anyways, here goes:
I have a Jobber and a Job and I have a relation between them - surprisingly called JobberJob
schema "jobbers_jobs" do
belongs_to :jobbers, Jobbers.Jobber, foreign_key: :jobbers_id, references: :jobbers_id,type: :string
belongs_to :jobs, Jobbers.Job, foreign_key: :jobs_id, references: :jobs_id,type: :string
timestamps()
end
schema "jobs" do
field :jobs_id, :string, primary_key: true
....
many_to_many :jobbers, Jobbers.Jobber,
join_through: Jobbers.JobberJob,
join_keys: [jobs_id: :jobs_id, jobbers_id: :jobbers_id],
on_replace: :delete,
on_delete: :delete_all,
unique: true # this does not, however, make the jobbers DISTINCT
timestamps()
end
schema "jobbers" do
field :jobbers_id, :string, primary_key: true
field :name, :string
...
end
When I do: job = Repo.get_by(Job, id: 1) |> Repo.preload :jobbers
this is what gets into my log:
[debug] QUERY OK source="jobs" db=0.7ms
SELECT j0.`id`, j0.`jobs_id`, j0.`teaser`, j0.`name`, j0.`job_when_name`, j0.`committee_name`, j0.`max_qty_jobbere`, j0.`min_qty_jobbere`, j0.`wanted_qty_jobbere`, j0.`inserted_at`, j0.`updated_at` FROM `jobs` AS j0 WHERE (j0.`id` = ?) [1]
[debug] QUERY OK source="jobbers" db=22.9ms decode=0.1ms
SELECT j0.`id`, j0.`jobbers_id`, j0.`name`, j0.`contact_address`, j0.`email`, j0.`mobile_clean`, j0.`workas_jobber`, j0.`state`, j0.`jobfunc`, j0.`create_date`, j0.`write_date`, j0.`birthdate`, j0.`country_code`, j0.`inserted_at`, j0.`updated_at`, j1.`jobs_id` FROM `jobbers` AS j0 INNER JOIN `jobs` AS j1 ON j1.`jobs_id` IN (?) INNER JOIN `jobbers_jobs` AS j2 ON j2.`jobs_id` = j1.`jobs_id` WHERE (j2.`jobbers_id` = j0.`jobbers_id`) ORDER BY j1.`jobs_id` ["__export__.campos_job_92"]
I suspect the “double-inner-join” to be the culprit behind providing TWO sets of jobbers - but I’m not able to disect it any better, I’m afraid
I really could do with a “second opinion” - like someone telling me to follow this link or that, read this book og that; or perhaps just plain and simple stick it to me: this will not work in Ecto, currently!
cheers,
Walther