ash-project/ash_sqlite

Cannot persist a float number

Closed this issue · 3 comments

Describe the bug
I'm getting (ArgumentError) cannot load 0.9 as type #Ash.Type.Float.EctoType<[]> when trying to persist a float number to an SQLite database.

To Reproduce
Using the following definitions:

defmodule AshFloatDomain do
  use Ash.Domain

  resources do
    resource AshFloatError
  end

end

defmodule AshFloatError do
  use Ash.Resource,
    domain: AshFloatDomain,
    data_layer: AshSqlite.DataLayer

  actions do
    defaults [:read]

    create :create do
      accept [:temperature]
    end
  end

  attributes do
    uuid_primary_key :id
    attribute :temperature, :float, allow_nil?: true
  end

  sqlite do
    table "ash_float_error"
    repo Elani.Repo
  end
end

after generating and running the migrations, this code produces the error:

iex(25)> AshFloatError |> Ash.Changeset.for_create(:create, %{temperature: 0.9}) |> Ash.create!()
** (Ash.Error.Unknown) 
Bread Crumbs:
  > Error returned from: AshFloatError.create

Unknown Error

* ** (ArgumentError) cannot load `0.9` as type #Ash.Type.Float.EctoType<[]> for field :temperature in #AshFloatError<__meta__: #Ecto.Schema.Metadata<:loaded, "ash_float_error">, id: nil, temperature: nil, aggregates: %{}, calculations: %{}, ...>
  (ecto 3.12.5) lib/ecto/repo/queryable.ex:431: Ecto.Repo.Queryable.struct_load!/6
  (ecto 3.12.5) lib/ecto/repo/schema.ex:76: anonymous fn/4 in Ecto.Repo.Schema.postprocess/5
  (elixir 1.18.0) lib/enum.ex:1714: Enum."-map/2-lists^map/1-1-"/2
  (ecto 3.12.5) lib/ecto/repo/schema.ex:61: Ecto.Repo.Schema.do_insert_all/7
  (ash_sqlite 0.2.1) lib/data_layer.ex:720: anonymous fn/4 in AshSqlite.DataLayer.bulk_create/3
  (elixir 1.18.0) lib/enum.ex:4964: Enumerable.List.reduce/3
  (elixir 1.18.0) lib/enum.ex:2600: Enum.reduce_while/3
  (ash_sqlite 0.2.1) lib/data_layer.ex:651: AshSqlite.DataLayer.bulk_create/3
    (ash 3.4.48) lib/ash/error/unknown.ex:3: Ash.Error.Unknown."exception (overridable 2)"/1
    (ash 3.4.48) /Users/victor/src/elani/deps/splode/lib/splode.ex:264: Ash.Error.to_class/2
    (ash 3.4.48) lib/ash/error/error.ex:108: Ash.Error.to_error_class/2
    (ash 3.4.48) lib/ash/actions/create/create.ex:161: Ash.Actions.Create.do_run/4
    (ash 3.4.48) lib/ash/actions/create/create.ex:50: Ash.Actions.Create.run/4
    (ash 3.4.48) lib/ash.ex:2134: Ash.create!/3
    iex:25: (file)

Expected behavior
I expect that I'm able to persist a float number.

** Runtime
ash 3.4.48
ash_sql 0.2.42
ash_sqlite 0.2.1
Elixir 1.18
Erlang/OTP 27
MacOS 15.2

Related post on Elixirforum

One other thing I noticed: the value is actually persisted to the database; so the bug might be somewhere in the deserializing code.

Commented on the elixir forum post about this, but from what I can tell this may be a bug in Ecto sql as this happens just using Repo.all on the resource.

Please create a reproduction using Repo.all and open an issue in ecto_sqlite3 with the reproduction.