Thanks, it has worked, however, I am trying to select only specific columns from father and mother, like:
query= from s in Schools.Student, where: s.school_id == ^school_id, join: f in assoc(s, :father), join: m in assoc(s, :mother), select: %{father_mobile: f.mobile_number, mother_mobile: m.mobile_number}
this would result into empty array, however, if I used preload as:
query= from s in Schools.Student, where: s.school_id == ^entity_id, preload: :father, preload: :mother
it will give all the students and their father, mother as expected.
so, why the first query resulted into empty array?
Yeah I’ll echo what @kokolegorille said. It seems like that approach to modelling will make some queries unnecessarily difficult.
I think what would be best is having a relationships table as a join table between students and parents. That way it is easy to query to see what students a parent is responsible and you could even set the relationship type to guardian, grandmother, or other.
But with that said I am not sure why you’d end up with an empty array from that query. Are you sure that there are some students in the school_id that you are querying?
Yes, I am sure, there are some students in the school_id that I am querying…
I will consider a pivot table that joins parents and students, currently if I needed to get a parent’s students, I do a query for parent_id in [ father_id, mother_id ] of students table