AE
Ash Elixir•2y ago
xSHYNE

manage_relationship argument not relating primary key to resource

resource
create :add_to_group do
argument :group, :uuid do
allow_nil? false
end

change manage_relationship(:group, type: :append_and_remove)
change relate_actor(:member)
end
end

relationships do
belongs_to(:group, Digsync.Accounts.Group, primary_key?: true, allow_nil?: false)
belongs_to(:member, Digsync.Accounts.User, primary_key?: true, allow_nil?: false)
end
create :add_to_group do
argument :group, :uuid do
allow_nil? false
end

change manage_relationship(:group, type: :append_and_remove)
change relate_actor(:member)
end
end

relationships do
belongs_to(:group, Digsync.Accounts.Group, primary_key?: true, allow_nil?: false)
belongs_to(:member, Digsync.Accounts.User, primary_key?: true, allow_nil?: false)
end
changeset after GroupMembership |> Ash.Changeset.for_create(:add_to_group, %{group: group_id})
#Ash.Changeset<
action_type: :create,
action: :add_to_group,
attributes: %{},
relationships: %{
group: [
{"b6937067-651d-4385-8323-20f193f03cd9",
[
ignore?: false,
on_missing: :unrelate,
on_match: :ignore,
on_lookup: :relate,
on_no_match: :error,
eager_validate_with: false,
authorize?: true,
meta: [inputs_was_list?: false, id: :group],
type: :append_and_remove
]}
],
member: [
{[
#Digsync.Accounts.User<
id: "b86d7074-a35a-4df6-8eac-a60327cc254f",
>
],
[
ignore?: false,
on_missing: :unrelate,
on_match: :ignore,
on_lookup: :relate,
on_no_match: :error,
eager_validate_with: false,
authorize?: true,
type: :append_and_remove,
meta: [inputs_was_list?: false]
]}
]
},
arguments: %{group: "b6937067-651d-4385-8323-20f193f03cd9"},
errors: [],
>,
valid?: true
>
#Ash.Changeset<
action_type: :create,
action: :add_to_group,
attributes: %{},
relationships: %{
group: [
{"b6937067-651d-4385-8323-20f193f03cd9",
[
ignore?: false,
on_missing: :unrelate,
on_match: :ignore,
on_lookup: :relate,
on_no_match: :error,
eager_validate_with: false,
authorize?: true,
meta: [inputs_was_list?: false, id: :group],
type: :append_and_remove
]}
],
member: [
{[
#Digsync.Accounts.User<
id: "b86d7074-a35a-4df6-8eac-a60327cc254f",
>
],
[
ignore?: false,
on_missing: :unrelate,
on_match: :ignore,
on_lookup: :relate,
on_no_match: :error,
eager_validate_with: false,
authorize?: true,
type: :append_and_remove,
meta: [inputs_was_list?: false]
]}
]
},
arguments: %{group: "b6937067-651d-4385-8323-20f193f03cd9"},
errors: [],
>,
valid?: true
>
** (Ash.Error.Invalid) Input Invalid

* record with "b6937067-651d-4385-8323-20f193f03cd9" not found
(ash 2.11.6) lib/ash/api/api.ex:2124: Ash.Api.unwrap_or_raise!/3
** (Ash.Error.Invalid) Input Invalid

* record with "b6937067-651d-4385-8323-20f193f03cd9" not found
(ash 2.11.6) lib/ash/api/api.ex:2124: Ash.Api.unwrap_or_raise!/3
I'm very confused, that id absolute exists and is a Group. When I do a different relationship which is coded almost identically it has a different data structure for the id:
action_type: :create,
action: :create_from_flow,
attributes: %{},
relationships: %{
friend_one: [
{[%{id: "266d68e7-3a8b-401a-a616-1b42c385a811"}],
action_type: :create,
action: :create_from_flow,
attributes: %{},
relationships: %{
friend_one: [
{[%{id: "266d68e7-3a8b-401a-a616-1b42c385a811"}],
11 Replies
ZachDaniel
ZachDaniel•2y ago
what is the primary read action on the group resource?
xSHYNE
xSHYNEOP•2y ago
default
actions do
defaults([:read, :update, :destroy])

create :create do
change relate_actor(:creator)
end
end
actions do
defaults([:read, :update, :destroy])

create :create do
change relate_actor(:creator)
end
end
ZachDaniel
ZachDaniel•2y ago
šŸ¤”
xSHYNE
xSHYNEOP•2y ago
iex(40)> Accounts.get!(Group, "b6937067-651d-4385-8323-20f193f03cd9")
** (Ash.Error.Invalid) Input Invalid

* invalid primary key "b6937067-651d-4385-8323-20f193f03cd9" provided
iex(40)> Accounts.get!(Group, "b6937067-651d-4385-8323-20f193f03cd9")
** (Ash.Error.Invalid) Input Invalid

* invalid primary key "b6937067-651d-4385-8323-20f193f03cd9" provided
digsync_dev=# select id from groups;
id
--------------------------------------
b6937067-651d-4385-8323-20f193f03cd9
(1 row)
digsync_dev=# select id from groups;
id
--------------------------------------
b6937067-651d-4385-8323-20f193f03cd9
(1 row)
ZachDaniel
ZachDaniel•2y ago
what about Ash.Type.cast_input(:uuid, "b6937067-651d-4385-8323-20f193f03cd9")? can I see the attributes section of Group?
xSHYNE
xSHYNEOP•2y ago
lemme try that
attributes do
uuid_primary_key(:id)

attribute :name, :string do
allow_nil? false

constraints min_length: 3,
trim?: true,
allow_empty?: false
end

attribute :description, :string do
allow_nil? false
end

attribute :locaton, :string

attribute :preferred_location, :string

attribute :type, :atom do
default :social
constraints one_of: [:club, :bar, :social]
end

create_timestamp(:inserted_at, private?: false, allow_nil?: false)
create_timestamp(:updated_at, private?: false, allow_nil?: false)
end
attributes do
uuid_primary_key(:id)

attribute :name, :string do
allow_nil? false

constraints min_length: 3,
trim?: true,
allow_empty?: false
end

attribute :description, :string do
allow_nil? false
end

attribute :locaton, :string

attribute :preferred_location, :string

attribute :type, :atom do
default :social
constraints one_of: [:club, :bar, :social]
end

create_timestamp(:inserted_at, private?: false, allow_nil?: false)
create_timestamp(:updated_at, private?: false, allow_nil?: false)
end
iex(3)> uuid = Ash.Type.cast_input(:uuid, "b6937067-651d-4385-8323-20f193f03cd9")
{:ok, "b6937067-651d-4385-8323-20f193f03cd9"}
iex(4)> {:ok, uuid} = v
{:ok, "b6937067-651d-4385-8323-20f193f03cd9"}
iex(5)> Accounts.get!(Group, uuid)
** (Ash.Error.Invalid) Input Invalid

* invalid primary key "b6937067-651d-4385-8323-20f193f03cd9" provided
(ash 2.11.6) lib/ash/filter/filter.ex:464: Ash.Filter.get_filter/2
iex(3)> uuid = Ash.Type.cast_input(:uuid, "b6937067-651d-4385-8323-20f193f03cd9")
{:ok, "b6937067-651d-4385-8323-20f193f03cd9"}
iex(4)> {:ok, uuid} = v
{:ok, "b6937067-651d-4385-8323-20f193f03cd9"}
iex(5)> Accounts.get!(Group, uuid)
** (Ash.Error.Invalid) Input Invalid

* invalid primary key "b6937067-651d-4385-8323-20f193f03cd9" provided
(ash 2.11.6) lib/ash/filter/filter.ex:464: Ash.Filter.get_filter/2
Table "public.groups"
Column | Type | Collation | Nullable | Default
--------------------+-----------------------------+-----------+----------+--------------------
id | uuid | | not null | uuid_generate_v4()
...
"groups_pkey" PRIMARY KEY, btree (id, creator_id)
Table "public.groups"
Column | Type | Collation | Nullable | Default
--------------------+-----------------------------+-----------+----------+--------------------
id | uuid | | not null | uuid_generate_v4()
...
"groups_pkey" PRIMARY KEY, btree (id, creator_id)
ZachDaniel
ZachDaniel•2y ago
do you have relationships set with primary_key?: true on Group? or anything that could make that a composite primary key. in iex Ash.Resource.Info.primary_key(Group)
xSHYNE
xSHYNEOP•2y ago
Ah I think I have two relationships
Ash.Resource.Info.primary_key(Group)
[:id, :creator_id]
Ash.Resource.Info.primary_key(Group)
[:id, :creator_id]
ZachDaniel
ZachDaniel•2y ago
šŸ‘ thats the issue there šŸ™‚ Do you want that to be the primary key of that resource or was it an accident?
xSHYNE
xSHYNEOP•2y ago
It looks like an accident but not sure how that happened Appreciate the quick help šŸ™‚
ZachDaniel
ZachDaniel•2y ago
my pleasure šŸ™‚

Did you find this page helpful?