Stefan Wintermeyer
Stefan Wintermeyer
AEAsh Elixir
Created by Stefan Wintermeyer on 9/24/2023 in #support
add_tag but unique
For the archive:
identities do
# identity :unique_name, [:name] <1>

identity :name, [:name] do
pre_check_with App.Shop
end
end
identities do
# identity :unique_name, [:name] <1>

identity :name, [:name] do
pre_check_with App.Shop
end
end
<1> Use with a PostgreSQL DB.
14 replies
AEAsh Elixir
Created by Stefan Wintermeyer on 9/24/2023 in #support
add_tag but unique
How does that work code wise? https://ash-hq.org/docs/dsl/ash-resource#identities-identity-pre_check_with doesn't include an example.
14 replies
AEAsh Elixir
Created by Stefan Wintermeyer on 9/24/2023 in #support
add_tag but unique
Do I use it wrong? Here's the tag resource:
defmodule App.Shop.Tag do
use Ash.Resource, data_layer: Ash.DataLayer.Ets

attributes do
uuid_primary_key :id
attribute :name, :string
end

identities do
identity :unique_name, [:name]
end

relationships do
many_to_many :products, App.Shop.Product do
through App.Shop.ProductTag
source_attribute_on_join_resource :tag_id
destination_attribute_on_join_resource :product_id
end
end

actions do
defaults [:read, :update, :destroy]

create :create do
primary? true
argument :products, {:array, :map}

change manage_relationship(:products,
type: :append_and_remove,
on_no_match: :create,
use_identities: [:unique_name]
)
end
end

code_interface do
define_for App.Shop
define :create
define :read
define :by_id, get_by: [:id], action: :read
define :by_name, get_by: [:name], action: :read
define :update
define :destroy
end
end
defmodule App.Shop.Tag do
use Ash.Resource, data_layer: Ash.DataLayer.Ets

attributes do
uuid_primary_key :id
attribute :name, :string
end

identities do
identity :unique_name, [:name]
end

relationships do
many_to_many :products, App.Shop.Product do
through App.Shop.ProductTag
source_attribute_on_join_resource :tag_id
destination_attribute_on_join_resource :product_id
end
end

actions do
defaults [:read, :update, :destroy]

create :create do
primary? true
argument :products, {:array, :map}

change manage_relationship(:products,
type: :append_and_remove,
on_no_match: :create,
use_identities: [:unique_name]
)
end
end

code_interface do
define_for App.Shop
define :create
define :read
define :by_id, get_by: [:id], action: :read
define :by_name, get_by: [:name], action: :read
define :update
define :destroy
end
end
With that I get this error:
** (EXIT from #PID<0.98.0>) an exception was raised:
** (Spark.Error.DslError) [App.Shop.Tag]
The data layer does not support native checking of identities.

Identities: unique_name

Must specify the `pre_check_with` option.

(spark 1.1.39) lib/spark/dsl/extension.ex:560: Spark.Dsl.Extension.raise_transformer_error/2
(elixir 1.15.5) lib/enum.ex:4830: Enumerable.List.reduce/3
(elixir 1.15.5) lib/enum.ex:2564: Enum.reduce_while/3
(elixir 1.15.5) lib/enum.ex:984: Enum."-each/2-lists^foreach/1-0-"/2
(elixir 1.15.5) lib/module/parallel_checker.ex:271: Module.ParallelChecker.check_module/3
(elixir 1.15.5) lib/module/parallel_checker.ex:82: anonymous fn/6 in Module.ParallelChecker.spawn/4
** (EXIT from #PID<0.98.0>) an exception was raised:
** (Spark.Error.DslError) [App.Shop.Tag]
The data layer does not support native checking of identities.

Identities: unique_name

Must specify the `pre_check_with` option.

(spark 1.1.39) lib/spark/dsl/extension.ex:560: Spark.Dsl.Extension.raise_transformer_error/2
(elixir 1.15.5) lib/enum.ex:4830: Enumerable.List.reduce/3
(elixir 1.15.5) lib/enum.ex:2564: Enum.reduce_while/3
(elixir 1.15.5) lib/enum.ex:984: Enum."-each/2-lists^foreach/1-0-"/2
(elixir 1.15.5) lib/module/parallel_checker.ex:271: Module.ParallelChecker.check_module/3
(elixir 1.15.5) lib/module/parallel_checker.ex:82: anonymous fn/6 in Module.ParallelChecker.spawn/4
14 replies
AEAsh Elixir
Created by Stefan Wintermeyer on 9/24/2023 in #support
add_tag but unique
Can you give me an example of how to setup an identity? https://hexdocs.pm/ash/identities.html doesn't have any examples and the https://ash-hq.org internal search engine doesn't work right now. Here's my tag resource:
defmodule App.Shop.Tag do
use Ash.Resource, data_layer: Ash.DataLayer.Ets

attributes do
uuid_primary_key :id
attribute :name, :string
end

relationships do
many_to_many :products, App.Shop.Product do
through App.Shop.ProductTag
source_attribute_on_join_resource :tag_id
destination_attribute_on_join_resource :product_id
end
end

actions do
defaults [:read, :update, :destroy]

create :create do
primary? true
argument :products, {:array, :map}
change manage_relationship(:products, type: :append_and_remove, on_no_match: :create)
end
end

code_interface do
define_for App.Shop
define :create
define :read
define :by_id, get_by: [:id], action: :read
define :by_name, get_by: [:name], action: :read
define :update
define :destroy
end
end
defmodule App.Shop.Tag do
use Ash.Resource, data_layer: Ash.DataLayer.Ets

attributes do
uuid_primary_key :id
attribute :name, :string
end

relationships do
many_to_many :products, App.Shop.Product do
through App.Shop.ProductTag
source_attribute_on_join_resource :tag_id
destination_attribute_on_join_resource :product_id
end
end

actions do
defaults [:read, :update, :destroy]

create :create do
primary? true
argument :products, {:array, :map}
change manage_relationship(:products, type: :append_and_remove, on_no_match: :create)
end
end

code_interface do
define_for App.Shop
define :create
define :read
define :by_id, get_by: [:id], action: :read
define :by_name, get_by: [:name], action: :read
define :update
define :destroy
end
end
14 replies
AEAsh Elixir
Created by Stefan Wintermeyer on 9/24/2023 in #support
How to add_tag? Managing Relationships
Thank you! I did not see that.
6 replies
AEAsh Elixir
Created by Stefan Wintermeyer on 9/23/2023 in #support
many_to_many update problem
Ahhh! Thanks! It works.
13 replies
AEAsh Elixir
Created by Stefan Wintermeyer on 9/23/2023 in #support
many_to_many update problem
I draw a blank here. The code already says:
create :update do
argument :tags, {:array, :map}
change manage_relationship(:tags, type: :append_and_remove, on_no_match: :create)
end
create :update do
argument :tags, {:array, :map}
change manage_relationship(:tags, type: :append_and_remove, on_no_match: :create)
end
Where would I include an other create?
13 replies
AEAsh Elixir
Created by Stefan Wintermeyer on 9/23/2023 in #support
many_to_many update problem
@Zach Daniel Can you help me out here? I'd like to record the tutorial.
13 replies
AEAsh Elixir
Created by Stefan Wintermeyer on 9/19/2023 in #showcase
Ash Resource - 3 minute introduction
No description
3 replies
AEAsh Elixir
Created by Stefan Wintermeyer on 9/23/2023 in #support
many_to_many update problem
I tried
iex(5)> App.Shop.Product.update!(banana, [yellow_tag])
** (FunctionClauseError) no function clause matching in anonymous fn/2 in Keyword.split/2
iex(5)> App.Shop.Product.update!(banana, [yellow_tag])
** (FunctionClauseError) no function clause matching in anonymous fn/2 in Keyword.split/2
But I don't know how to do the args: [tags] thing and I am lost in the documentation.
13 replies
AEAsh Elixir
Created by Stefan Wintermeyer on 9/21/2023 in #showcase
Tutorial: belongs_to in 2 minutes
Thanks for the feedback. I'll add the version number. About the speed: It is meant to be stopped, repeated and slowed down.
4 replies
AEAsh Elixir
Created by Stefan Wintermeyer on 9/20/2023 in #support
How to create a product with many_to_many tags?
Thanks! Solution for the archive:
defmodule App.Shop.ProductTag do
use Ash.Resource, data_layer: Ash.DataLayer.Ets

actions do
defaults [:create, :read, :update, :destroy]
end

relationships do
belongs_to :product, App.Shop.Product do
primary_key? true
allow_nil? false
end

belongs_to :tag, App.Shop.Tag do
primary_key? true
allow_nil? false
end
end
end
defmodule App.Shop.ProductTag do
use Ash.Resource, data_layer: Ash.DataLayer.Ets

actions do
defaults [:create, :read, :update, :destroy]
end

relationships do
belongs_to :product, App.Shop.Product do
primary_key? true
allow_nil? false
end

belongs_to :tag, App.Shop.Tag do
primary_key? true
allow_nil? false
end
end
end
iex(1)> good_deal_tag = App.Shop.Tag.create!(%{name: "Good deal"})
iex(2)> yellow_tag = App.Shop.Tag.create!(%{name: "Yellow"})
iex(3)> App.Shop.Product.create!(%{name: "Banana", tags: [good_deal_tag, yellow_tag]})
iex(4)> App.Shop.Product.by_name!("Banana", load: [:tags])
#App.Shop.Product<
tags: [
#App.Shop.Tag<
products: #Ash.NotLoaded<:relationship>,
products_join_assoc: #Ash.NotLoaded<:relationship>,
__meta__: #Ecto.Schema.Metadata<:loaded>,
id: "82b7e8af-69b9-4f35-b32a-0b6b2bed1d15",
name: "Good deal",
aggregates: %{},
calculations: %{},
...
>,
#App.Shop.Tag<
products: #Ash.NotLoaded<:relationship>,
products_join_assoc: #Ash.NotLoaded<:relationship>,
__meta__: #Ecto.Schema.Metadata<:loaded>,
id: "d04aa5ef-195e-4dd8-9c5a-5c73e6f44afe",
name: "Yellow",
aggregates: %{},
calculations: %{},
...
>
],
...
>
iex(1)> good_deal_tag = App.Shop.Tag.create!(%{name: "Good deal"})
iex(2)> yellow_tag = App.Shop.Tag.create!(%{name: "Yellow"})
iex(3)> App.Shop.Product.create!(%{name: "Banana", tags: [good_deal_tag, yellow_tag]})
iex(4)> App.Shop.Product.by_name!("Banana", load: [:tags])
#App.Shop.Product<
tags: [
#App.Shop.Tag<
products: #Ash.NotLoaded<:relationship>,
products_join_assoc: #Ash.NotLoaded<:relationship>,
__meta__: #Ecto.Schema.Metadata<:loaded>,
id: "82b7e8af-69b9-4f35-b32a-0b6b2bed1d15",
name: "Good deal",
aggregates: %{},
calculations: %{},
...
>,
#App.Shop.Tag<
products: #Ash.NotLoaded<:relationship>,
products_join_assoc: #Ash.NotLoaded<:relationship>,
__meta__: #Ecto.Schema.Metadata<:loaded>,
id: "d04aa5ef-195e-4dd8-9c5a-5c73e6f44afe",
name: "Yellow",
aggregates: %{},
calculations: %{},
...
>
],
...
>
18 replies
AEAsh Elixir
Created by Stefan Wintermeyer on 9/20/2023 in #support
How to create a product with many_to_many tags?
defmodule App.Shop.ProductTag do
use Ash.Resource, data_layer: Ash.DataLayer.Ets

relationships do
belongs_to :product, App.Shop.Product do
primary_key? true
allow_nil? false
end

belongs_to :tag, App.Shop.Tag do
primary_key? true
allow_nil? false
end
end
end
defmodule App.Shop.ProductTag do
use Ash.Resource, data_layer: Ash.DataLayer.Ets

relationships do
belongs_to :product, App.Shop.Product do
primary_key? true
allow_nil? false
end

belongs_to :tag, App.Shop.Tag do
primary_key? true
allow_nil? false
end
end
end
That results in this error:
$ iex -S mix
Compiling 2 files (.ex)
** (EXIT from #PID<0.98.0>) an exception was raised:
** (Spark.Error.DslError) [App.Shop.Product]
actions -> create -> create -> change -> manage_relationship -> tags:
The following error was raised when validating options provided to manage_relationship.

** (RuntimeError) Required primary create action for App.Shop.ProductTag.
(ash 2.14.17) lib/ash/resource/info.ex:493: Ash.Resource.Info.primary_action!/2
...
$ iex -S mix
Compiling 2 files (.ex)
** (EXIT from #PID<0.98.0>) an exception was raised:
** (Spark.Error.DslError) [App.Shop.Product]
actions -> create -> create -> change -> manage_relationship -> tags:
The following error was raised when validating options provided to manage_relationship.

** (RuntimeError) Required primary create action for App.Shop.ProductTag.
(ash 2.14.17) lib/ash/resource/info.ex:493: Ash.Resource.Info.primary_action!/2
...
18 replies
AEAsh Elixir
Created by Stefan Wintermeyer on 9/20/2023 in #support
How to create a product with many_to_many tags?
Here's the current code.
defmodule App.Shop.Tag do
use Ash.Resource, data_layer: Ash.DataLayer.Ets

attributes do
uuid_primary_key :id
attribute :name, :string
end

relationships do
many_to_many :products, App.Shop.Product do
through App.Shop.ProductTag
source_attribute_on_join_resource :tag_id
destination_attribute_on_join_resource :product_id
end
end

actions do
defaults [:read, :update, :destroy]

create :create do
primary? true
argument :products, {:array, :map}
change manage_relationship(:products, type: :append_and_remove, on_no_match: :create)
end
end

code_interface do
define_for App.Shop
define :create
define :read
define :by_id, get_by: [:id], action: :read
define :by_name, get_by: [:name], action: :read
define :update
define :destroy
end
end
defmodule App.Shop.Tag do
use Ash.Resource, data_layer: Ash.DataLayer.Ets

attributes do
uuid_primary_key :id
attribute :name, :string
end

relationships do
many_to_many :products, App.Shop.Product do
through App.Shop.ProductTag
source_attribute_on_join_resource :tag_id
destination_attribute_on_join_resource :product_id
end
end

actions do
defaults [:read, :update, :destroy]

create :create do
primary? true
argument :products, {:array, :map}
change manage_relationship(:products, type: :append_and_remove, on_no_match: :create)
end
end

code_interface do
define_for App.Shop
define :create
define :read
define :by_id, get_by: [:id], action: :read
define :by_name, get_by: [:name], action: :read
define :update
define :destroy
end
end
defmodule App.Shop.Product do
use Ash.Resource, data_layer: Ash.DataLayer.Ets

attributes do
uuid_primary_key :id
attribute :name, :string
attribute :price, :decimal
end

relationships do
many_to_many :tags, App.Shop.Tag do
through App.Shop.ProductTag
source_attribute_on_join_resource :product_id
destination_attribute_on_join_resource :tag_id
end
end

actions do
defaults [:read, :update, :destroy]

create :create do
primary? true
argument :tags, {:array, :map}
change manage_relationship(:tags, type: :append_and_remove, on_no_match: :create)
end
end

code_interface do
define_for App.Shop
define :create
define :read
define :by_id, get_by: [:id], action: :read
define :by_name, get_by: [:name], action: :read
define :update
define :destroy
end
end
defmodule App.Shop.Product do
use Ash.Resource, data_layer: Ash.DataLayer.Ets

attributes do
uuid_primary_key :id
attribute :name, :string
attribute :price, :decimal
end

relationships do
many_to_many :tags, App.Shop.Tag do
through App.Shop.ProductTag
source_attribute_on_join_resource :product_id
destination_attribute_on_join_resource :tag_id
end
end

actions do
defaults [:read, :update, :destroy]

create :create do
primary? true
argument :tags, {:array, :map}
change manage_relationship(:tags, type: :append_and_remove, on_no_match: :create)
end
end

code_interface do
define_for App.Shop
define :create
define :read
define :by_id, get_by: [:id], action: :read
define :by_name, get_by: [:name], action: :read
define :update
define :destroy
end
end
18 replies
AEAsh Elixir
Created by Stefan Wintermeyer on 9/20/2023 in #support
How to create a product with many_to_many tags?
actions do
defaults [:read, :update, :destroy]

create :create do
argument :tags, {:array, :map}
change manage_relationship(:tags, type: :append_and_remove, on_no_match: :create)
end
end
actions do
defaults [:read, :update, :destroy]

create :create do
argument :tags, {:array, :map}
change manage_relationship(:tags, type: :append_and_remove, on_no_match: :create)
end
end
This raises an error:
$ iex -S mix
Compiling 2 files (.ex)
** (EXIT from #PID<0.98.0>) an exception was raised:
** (Spark.Error.DslError) [App.Shop.Product]
actions -> create -> create -> change -> manage_relationship -> tags:
The following error was raised when validating options provided to manage_relationship.

** (RuntimeError) Required primary create action for App.Shop.ProductTag.
[...]
$ iex -S mix
Compiling 2 files (.ex)
** (EXIT from #PID<0.98.0>) an exception was raised:
** (Spark.Error.DslError) [App.Shop.Product]
actions -> create -> create -> change -> manage_relationship -> tags:
The following error was raised when validating options provided to manage_relationship.

** (RuntimeError) Required primary create action for App.Shop.ProductTag.
[...]
18 replies
AEAsh Elixir
Created by Stefan Wintermeyer on 9/20/2023 in #support
How to create a product with many_to_many tags?
Where exactly do I have to put this code?
argument :tags, {:array, :map}
change manage_relationship(:tags, type: :append_and_remove}
argument :tags, {:array, :map}
change manage_relationship(:tags, type: :append_and_remove}
18 replies
AEAsh Elixir
Created by Stefan Wintermeyer on 9/20/2023 in #support
How to create a product with many_to_many tags?
Is the later possible too?
18 replies
AEAsh Elixir
Created by Stefan Wintermeyer on 9/20/2023 in #support
How to create a product with a category id?
@barnabasj Since I am writing beginners documentation I'd like to know if I understand it right: Code like App.Shop.Product.create!(%{name: "Orange", price: 0.20, category: %{name: "Fruits"}} is not the Ash way or is it? I am not so much interested in what is possible by opening all escape hatches but what is the intended way of doing it. I have one new Category and I have one new Product. What is the fastest way of creating it? What is the Ash way of doing it?
15 replies
AEAsh Elixir
Created by Stefan Wintermeyer on 9/20/2023 in #support
How to create a product with a category id?
Thanks @barnabasj! For the archive. Here's the fixed code and how it's working:
relationships do
belongs_to :category, App.Shop.Category do
attribute_writable? true
end
end
relationships do
belongs_to :category, App.Shop.Category do
attribute_writable? true
end
end
iex(13)> orange = App.Shop.Product.create!(%{name: "Orange", price: 0.20, category_id: fruits.id})
#App.Shop.Product<
category: #Ash.NotLoaded<:relationship>,
__meta__: #Ecto.Schema.Metadata<:loaded>,
id: "40cc21c2-58f8-4db4-8d98-7ab7c1741fa8",
name: "Orange",
price: Decimal.new("0.2"),
category_id: "5b582e8a-3c19-4ce4-b609-88748a46e2ce",
aggregates: %{},
calculations: %{},
...
>
iex(13)> orange = App.Shop.Product.create!(%{name: "Orange", price: 0.20, category_id: fruits.id})
#App.Shop.Product<
category: #Ash.NotLoaded<:relationship>,
__meta__: #Ecto.Schema.Metadata<:loaded>,
id: "40cc21c2-58f8-4db4-8d98-7ab7c1741fa8",
name: "Orange",
price: Decimal.new("0.2"),
category_id: "5b582e8a-3c19-4ce4-b609-88748a46e2ce",
aggregates: %{},
calculations: %{},
...
>
This doesn't work:
App.Shop.Product.create!(%{name: "Orange", price: 0.20, category: fruits})
App.Shop.Product.create!(%{name: "Orange", price: 0.20, category: fruits})
Should it but I configured the resource wrong or shouldn't it never because this isn't ActiveRecord?
15 replies
AEAsh Elixir
Created by Stefan Wintermeyer on 9/20/2023 in #support
How to create a product with a category id?
What is the right syntax for that? https://ash-hq.org/docs/dsl/ash-resource#relationships-belongs_to-attribute_writable- doesn't contain an example. This doesn't work:
relationships do
belongs_to :category, App.Shop.Category do
writeable?: true
end
end
relationships do
belongs_to :category, App.Shop.Category do
writeable?: true
end
end
15 replies