How to use primary key implicit identity as upsert_identity value?

I have the following many-to-many resource in my project:

defmodule Core.Feedbacks.TeacherClass do
  use Ash.Resource,
    domain: Core.Feedbacks,
    data_layer: AshPostgres.DataLayer

  relationships do
    alias Core.Feedbacks.{Teacher, Class}

    belongs_to :teacher, Teacher do
      primary_key? true
      allow_nil? false
      public? true

      source_attribute :user_id
    end

    belongs_to :class, Class do
      primary_key? true
      allow_nil? false
      public? true
    end
  end

  postgres do
    table "users_classes"

    references do
      reference :teacher, on_update: :update, on_delete: :delete
      reference :class, on_update: :update, on_delete: :delete
    end

    repo Core.Repo
  end
end

This resource will create a implicit unique index in postgres for columns user_id and class_id called users_classes_pkey.

I want to use that index as the upsert_identity value for my create action. I can create a new identity, but I think this will actually create a duplicated index in the DB if I recall it correctly.

So, what should I use in upsert_identity to use that index as the upsert condition?

Seems like if I don’t set the upsert_identity value at all, it will use the implicit index by default.

Is that correct? Or would that potentially break in the future?

1 Like

that is correct :slight_smile:

1 Like