manage_relationship argument not relating primary key to resource

xshyne88
2023-07-13

xshyne88:

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

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.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"}],

zachdaniel:

what is the primary read action on the group resource?

xshyne88:

default

xshyne88:

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

    create :create do
      change relate_actor(:creator)
    end
  end

zachdaniel:

šŸ¤”

xshyne88:

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

* invalid primary key "b6937067-651d-4385-8323-20f193f03cd9" provided

xshyne88:

digsync_dev=# select id from groups;
                  id
--------------------------------------
 b6937067-651d-4385-8323-20f193f03cd9
(1 row)

zachdaniel:

what about Ash.Type.cast_input(:uuid, "b6937067-651d-4385-8323-20f193f03cd9") ?

zachdaniel:

can I see the attributes section of Group ?

xshyne88:

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

xshyne88:

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

xshyne88:

                                    Table "public.groups"
       Column       |            Type             | Collation | Nullable |      Default
--------------------+-----------------------------+-----------+----------+--------------------
 id                 | uuid                        |           | not null | uuid_generate_v4()
...
   "groups_pkey" PRIMARY KEY, btree (id, creator_id)

zachdaniel:

do you have relationships set with primary_key?: true on Group ?

zachdaniel:

or anything that could make that a composite primary key. in iex Ash.Resource.Info.primary_key(Group)

xshyne88:

Ah I think I have two relationships

xshyne88:

[:id, :creator_id]

zachdaniel:

šŸ‘ thats the issue there šŸ™‚

zachdaniel:

Do you want that to be the primary key of that resource or was it an accident?

xshyne88:

It looks like an accident but not sure how that happened

xshyne88:

Appreciate the quick help

xshyne88:

šŸ™‚

zachdaniel:

my pleasure šŸ™‚