I have an Ecto schema that includes field :owned_by_id, :string
. I declared the field a string because I need to support values like "abc123" as well as values like "123".
The docs for cast/3 say:
The second argument is a map of params that are cast according to the type information from
data
.
In my module, I define changeset
like:
def changeset(struct, params \\ %{}) do
cast(struct, params, [:owned_by_id])
end
When I do this:
MyModule.changeset(%MyModule{}, %{owned_by_id: 1})
... I would expect cast
to turn that owned_by_id
integer param into a string, based on the field
declaration.
However, what I get instead is a changeset that includes
errors: [owned_by_id: {"is invalid", [type: :string]}]
I could call Integer.to_string(1)
myself, but shouldn't cast
handle that? Is there a way to have it handle this automatically?