fummmp
fummmp
AEAsh Elixir
Created by fummmp on 1/28/2023 in #support
Create Resource without relation
Hey guys, I'm dipping my toes into Ash and try to build a small thing, but now I'm stuck. I created a resource and a relationship. I want to create the resource, but the relationship can be empty. But when I try to create the resource, I get this error:
iex(5)> Inventizr.Inventory.Item |> Ash.Changeset.for_create(:new, %{model: "MacBook", brand: "Apple"}) |> Inventizr.Inventory.create!()
[debug] QUERY OK db=2.3ms idle=1636.8ms
begin []
[debug] QUERY OK db=2.4ms
rollback []
** (UndefinedFunctionError) function Ash.NotLoaded.__changeset__/0 is undefined or private
(ash 2.5.10) Ash.NotLoaded.__changeset__()
(ecto 3.9.4) lib/ecto/changeset.ex:409: Ecto.Changeset.change/2
(ecto 3.9.4) lib/ecto/changeset/relation.ex:173: Ecto.Changeset.Relation.do_change/4
(ecto 3.9.4) lib/ecto/changeset/relation.ex:335: Ecto.Changeset.Relation.single_change/5
(ecto 3.9.4) lib/ecto/changeset/relation.ex:165: Ecto.Changeset.Relation.change/3
...
iex(5)> Inventizr.Inventory.Item |> Ash.Changeset.for_create(:new, %{model: "MacBook", brand: "Apple"}) |> Inventizr.Inventory.create!()
[debug] QUERY OK db=2.3ms idle=1636.8ms
begin []
[debug] QUERY OK db=2.4ms
rollback []
** (UndefinedFunctionError) function Ash.NotLoaded.__changeset__/0 is undefined or private
(ash 2.5.10) Ash.NotLoaded.__changeset__()
(ecto 3.9.4) lib/ecto/changeset.ex:409: Ecto.Changeset.change/2
(ecto 3.9.4) lib/ecto/changeset/relation.ex:173: Ecto.Changeset.Relation.do_change/4
(ecto 3.9.4) lib/ecto/changeset/relation.ex:335: Ecto.Changeset.Relation.single_change/5
(ecto 3.9.4) lib/ecto/changeset/relation.ex:165: Ecto.Changeset.Relation.change/3
...
The resource is defined as:
defmodule Inventizr.Inventory.Item do
use Ash.Resource,
data_layer: AshPostgres.DataLayer

attributes do
uuid_primary_key :id

attribute :brand, :string
attribute :model, :string

attribute :status, :atom do
constraints one_of: [:available, :in_use, :defect, :unavailable]
default :available
allow_nil? false
end
end

relationships do
belongs_to :user, Inventizr.Accounts.User do
api Inventizr.Accounts
allow_nil? true
end
end
end
defmodule Inventizr.Inventory.Item do
use Ash.Resource,
data_layer: AshPostgres.DataLayer

attributes do
uuid_primary_key :id

attribute :brand, :string
attribute :model, :string

attribute :status, :atom do
constraints one_of: [:available, :in_use, :defect, :unavailable]
default :available
allow_nil? false
end
end

relationships do
belongs_to :user, Inventizr.Accounts.User do
api Inventizr.Accounts
allow_nil? true
end
end
end
And the corresponding relation:
attributes do
uuid_primary_key :id
attribute :name, :string
end

relationships do
has_many :items, Inventizr.Inventory.Item do
api Inventizr.Inventory
end
end
end
attributes do
uuid_primary_key :id
attribute :name, :string
end

relationships do
has_many :items, Inventizr.Inventory.Item do
api Inventizr.Inventory
end
end
end
I can't figure out, what I'm doing wrong, because it works like this in the getting started guide: https://ash-hq.org/docs/guides/ash/latest/tutorials/get-started#working-with-relationships. I'm sure I'm missing something obvious...
34 replies