Hello,
I must be missing something very simple, but I’m really puzzled by this inability to create a resource with a float number. I’m using sqlite, and I have 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
I ran ash_sqlite.generate_migrations
, which generated the following migration:
defmodule Elani.Repo.Migrations.MigrateResources3 do
@moduledoc """
Updates resources based on their most recent snapshots.
This file was autogenerated with `mix ash_sqlite.generate_migrations`
"""
use Ecto.Migration
def up do
create table(:ash_float_error, primary_key: false) do
add :temperature, :float
add :id, :uuid, null: false, primary_key: true
end
end
def down do
drop table(:ash_float_error)
end
end
and after running ash_sqlite.migrate
, I can verify the the table was created properly:
sqlite3 .elani.db
-- Loading resources from /Users/victor/.sqliterc
SQLite version 3.43.2 2023-10-10 13:08:14
Enter ".help" for usage hints.
sqlite> .schema ash_float_error
CREATE TABLE IF NOT EXISTS "ash_float_error" ("temperature" NUMERIC, "id" TEXT NOT NULL PRIMARY KEY);
When creating a changeset, I can see that it’s valid:
iex(24)> AshFloatError |> Ash.Changeset.for_create(:create, %{temperature: 0.9})
#Ash.Changeset<
domain: AshFloatDomain,
action_type: :create,
action: :create,
attributes: %{temperature: 0.9},
relationships: %{},
errors: [],
data: #AshFloatError<
__meta__: #Ecto.Schema.Metadata<:built, "ash_float_error">,
id: nil,
temperature: nil,
aggregates: %{},
calculations: %{},
...
>,
valid?: true
>
yet when I’m trying to persist, I get the following 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)
Does it look like a bug? Should I file a bug report in ash_sqlite
?
I’m using
ash 3.4.48
ash_sql 0.2.42
ash_sqlite 0.2.1
which I think are the latest version.
Many thanks for helping to debug this,
Victor.