I have a CSV file uploaded, parsed and inserted into DB, all is fine,
data in the CSV row is:
name, father_mobile, mother_mobile
I want to insert name
into students
table, while father_mobile
, mother_mobile
into parents table as separate records, as each student has two foreign keys, father_id
& mother_id
The relation is student has_one mother, has_one father, through father_id
& mother_id
, so, I will created three records from one CSV row, student& two parents.
So far, I have achieved this:
// collect fathers:
father_rows= Enum.map(ecto_rows, fn %{father_mobile: father_mobile} -> %{parent_type: "father", mobile_number: father_mobile} end)
// collect mothers:
mother_rows= Enum.map(ecto_rows, fn %{mother_mobile: mother_mobile} -> %{parent_type: "mother", mobile_number: mother_mobile} end)
// drop from students:
students_rows= Enum.map(ecto_rows, fn row -> Map.pop(row, "father_mobile") Map.pop(row, "mother_mobile") end)
Now, I have three maps ready to be inserted into DB, but how would I achieve the association?
I have tried Repo.insert_all
successfully for students, but, how would I associated fathers and mothers?
I thought of iterating on maps one by one, but how would I do so?
What’s your thoughts about it?