Ecto: how to cast an array/list in a changeset

Hi forum!

I’m quite new to elixir and phoenix and really like it so far. But there’s one thing I don’t understand, here’s my code:

Mutation:

  def change do
    create table(:things) do
      add :int_data, {:array, :int}
      add :error, :string

      timestamps()
    end

Ecto module

  schema "things" do
    field :error, :string
    field :int_data, {:array, :int}

    timestamps()
  end

How is the correct changeset implementation for it?

  def changeset(observation, attrs) do
    observation
    |> cast(attrs, [:error, :int_data])
  end

results in

** (UndefinedFunctionError) function :int.cast/1 is undefined or private
    :int.cast(22)
    (ecto 3.4.6) lib/ecto/type.ex:1233: Ecto.Type.array/3
    (ecto 3.4.6) lib/ecto/changeset.ex:559: Ecto.Changeset.cast_field/8
    (ecto 3.4.6) lib/ecto/changeset.ex:520: Ecto.Changeset.process_param/7
    (elixir 1.10.4) lib/enum.ex:2111: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto 3.4.6) lib/ecto/changeset.ex:497: Ecto.Changeset.cast/6

for input [22, 25, 80, 443, 587, 993]

Thank you in advance!

Hello and welcome,

Maybe try :integer instead :slight_smile:

In the middle of this document page, You’ll find all primitive types

4 Likes

ah, yes :man_facepalming:
thank you!