Sorry for necroing, but i would probably solve this by writing directly to the many-to-many table
%MyApp.Context.UserCompanies{}
|> MyApp.Contex.UserCompanies.changeset(%{company_id: exsisting_company_id, user_id: existing_user_id})
|> MyApp.Repo.insert!
Is there any reason this won't work?