Ecto: Ensure field default values match type

Created on 16 Nov 2020  路  3Comments  路  Source: elixir-ecto/ecto

Currently we don't do that:

defmodule Person do
  use Ecto.Schema

  schema "people" do
    field :age, :integer, default: "10"
  end
end

defmodule PersonTest do
  use ExUnit.Case, async: true

  test "it works" do
    Ecto.TestRepo.insert!(%Person{})
    # ** (Ecto.ChangeError) value `"10"` for `Person.age` in `insert` does not match type :integer
  end
end

I think we should raise an error during compilation instead.

Similar to https://github.com/dashbitco/nimble_options/pull/50.

Intermediate

Most helpful comment

Yup, go for it!

All 3 comments

Sounds good. We should call dump on it to see if it can be dumped.

Can I take this one?

Yup, go for it!

Was this page helpful?
0 / 5 - 0 ratings