Defect: Null: False used to define NOT NULL Column is throwing compilation error in 3.8 ECTO Version

In Ecto 3.5.x version we use null: false in the Ecto schema to create not null column in postgress database.

Now if we use the same in 3.8 the code is not even compiling.

Error stack is something like

** (ArgumentError) invalid option :null for field/3
(ecto 3.8.2) lib/ecto/schema.ex:2214: Ecto.Schema.check_options!/3
(ecto 3.8.2) lib/ecto/schema.ex:1911: Ecto.Schema.field/4
lib/tradeserf/model/oneoff_acct_group.ex:9: (module)
(stdlib 3.17) erl_eval.erl:685: :erl_eval.do_apply/6
(elixir 1.12.3) lib/kernel/parallel_compiler.ex:319: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7

Definition is :

schema “oneoff_acct_group” do
field(:acct_group_id, :string, primary_key: true)
field(:acct_group_nm, :string, null: false)
field(:entity_id, :string)
field(:owner, :string)

timestamps()

end

This is not a bug, as fields never supported a null option to begin with. It’s only a supported option for add in migrations. The thing, which changed is that ecto now actually validates options instead of silently ignoring them, so you can fix them.

5 Likes
defp check_options!(opts, valid, fun_arity) do
    case Enum.find(opts, fn {k, _} -> not(k in valid) end) do
      {k, _} -> raise ArgumentError, "invalid option #{inspect k} for #{fun_arity}"
      nil -> :ok
    end
  end

  defp check_options!({:parameterized, _, _}, _opts, _valid, _fun_arity) do
    :ok
  end

  defp check_options!({_, type}, opts, valid, fun_arity) do
    check_options!(type, opts, valid, fun_arity)
  end

  defp check_options!(_type, opts, valid, fun_arity) do
    check_options!(opts, valid, fun_arity)
  end

I am not sure but I am guessing that
Ecto 3.8.2 (I guess) check_option fuction has been changed.

before that version, it was not raising exception because it’s not filtered by Enum.find(opts, fn {k, _} → not(k in valid) end) at first.