Ecto Many-To-Many insertion with additional attributes in association table, need help with approach

Welcome to Elixir Forum.

This works for while inserting one student, but when inserting next record the Teacher would exist already and there is a chance it will insert multiple records for the same Teacher.

You just need to check for this. Either a database constraint for uniqueness of combination or in your elixir code.

For many to many with existing record I believe you need to explicitly use join table and use cast_assoc put_assoc. This imply that you need to code out explicitly the join table as a schema. I know you don’t have to in other cases but so far in my experience I’ve made schema for all my join tables since I often use one of the two tables as lookup table.

You can see my thread I made here:

2 Likes