Getting Ecto schema error: (ArgumentError) invalid option :size for field/3

hi everyone I write a program

defmodule Houshold.HtmlApi.Device.DeviceSchema do
  use Ecto.Schema
  import Ecto.Changeset
  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id
  schema "devices" do
    field :name, :string, size: 150, null: false
    timestamps()
  end
  def changeset(device, params \\ %{}) do
    device
    |> cast(params, [:name])
    |> validate_required([:name], message: "fill all boxes")
  end
end

but when I test it using mix test I received this message

\houshold>mix test
Compiling 1 file (.ex)

== Compilation error in file lib/houshold/html_api/admin/device/device_schema.ex ==
** (ArgumentError) invalid option :size for field/3
    (ecto 3.12.5) lib/ecto/schema.ex:2454: Ecto.Schema.check_options!/3
    (ecto 3.12.5) lib/ecto/schema.ex:2079: Ecto.Schema.__field__/4
    lib/houshold/html_api/admin/device/device_schema.ex:7: (module)

how I can fix it?

Welcome! Those simply aren’t valid options to pass to a field definition. If you’re trying to set a database level constraint do so in the migration, and if you’re trying to do an Elixir lev validation use the changeset functions.

3 Likes

Thank you