My code is as follows:
defp import_product(importer, head, product) do
IO.inspect("import_product")
head_a = Enum.map(Enum.slice(head, 0, 10), &String.to_existing_atom/1)
product_a = Enum.slice(product, 0, 10)
IO.inspect(head_a, label: "header length")
IO.inspect(product_a, label: "product length")
params = Map.new(List.zip([head_a, product_a]))
IO.inspect(params,label: "params_new")
changeset = Ecto.Changeset.cast(%Mirror.Imports.Product{}, params, []) # <-- problem this line
IO.inspect(changeset, label: "changeset_new")
product_new = Ecto.build_assoc(importer, :products, changeset)
Repo.insert!(product_new)
end
I found changeset in the above marked line contain no fields. The fourth parameter []
means no permitted fields allowed to be changed, not all fields are permitted.
How to set all fields permitted simply?
One can use Ecto.Schema
reflection as __schema__(:fields)
.
%Mirror.Imports.Product{}
|> Ecto.Changeset.cast(params, __schema__(:fields))