The error you quoted sounds like a Phoenix HTML error message. Presumably you are using the result of the register function and assigning it some kind of socket.assigns.changeset or something like that? The problem is happening then because you are assigning a Company changeset where it should be a User changeset.
I can suggest a straightforward solution. In your view (is it a LiveView?) where you call the register function and handle the result, pattern match the result to see if it is a Company changeset. Then do something like:
update(socket, :changeset, fn changeset -> Ecto.Changeset.put_change(changeset, :company, company_changeset end)
It is difficult for me to imagine a better approach because there are too many unknowns. It would be cleaner if you could cast_assoc the user on the company or vice-versa but without knowing what the create_company Multi does, and without knowing if a user can have many companies, or vice versa, it is difficult to advise. But cast_assoc works well with associations in forms. It is where the sort_param and drop_param magic live. I strongly advise using cast_assoc if you can.
It might be a good exercise to try to add a @spec to your register function. This will force you to think about the possible outputs.
It might also be worthwhile to write the register function as a single Multi pipeline, without the nested Repo.transaction in create_company. You can keep create_company as it is written but split out the ecto multi part so you have a function like this:
defmodule Companies do
def create_company(attrs) do
Multi.new()
|> create_company_multi(attrs)
|> Repo.transaction()
end
def create_company_multi(multi, attrs) do
multi
# |> Multi.run(:insert, ...)
end
end
Then you can rewrite register to pipe into the create_company_multi function.






















