Ash FrameworkAF
Ash Framework3y ago
19 replies
Robert Graff

Understanding value_is_key option when managing relationships

Given a relation that uses a ci_string (not uuid):
    belongs_to :feature, Environments.Feature do
      allow_nil? false
      source_attribute :feature_key
      attribute_type :ci_string
      destination_attribute :key
    end


I would expect this change to work using value_is_key option:
      argument :feature_key, :ci_string, allow_nil?: false
      change manage_relationship(:feature_key, :feature,
               type: :append,
               value_is_key: :key,
               use_identities: [:key]
             )


But it gives me this error, but I thought the purpose of
value_is_key
is to not provide a struct:
     ** (Ash.Error.Invalid) Input Invalid
     * Invalid value provided for feature: cannot provide structs that don't match the destination.


For reference, this more verbose change does work:
      argument :feature_key, :ci_string, allow_nil?: false

      change fn changeset, _ ->
        case Ash.Changeset.get_argument(changeset, :feature_key) do
          nil ->
            changeset

          feature_key ->
            Ash.Changeset.manage_relationship(
              changeset,
              :feature,
              %{key: feature_key},
              type: :append,
              use_identities: [:key]
            )
        end
      end
Was this page helpful?