Query deep associations

Say i have a a schema where A has a many_to_many relationship with B which has a many_to_many relationship with C. So these are the tables

  • A
  • A_B
  • B
  • B_C
  • C

I did not want to define any schema for association tables A_B, B_C. I’m using join_through: "A_B" to define many_to_many relationships.
I want to create an association in A has_many C which is essentially A join A_B join B join B_C join C. Is it possible to use schema definitions like join or join_through to achieve this or do i have to resort to preload queries Ecto.Query — Ecto v3.10.3 to do this?

You should be able to do has_many :c, through: [:b, :c] in A assuming you have your :join_through associations set up properly in A and B.

2 Likes