Possible to use relationships for Identity?

defmodule One.Exam.QuestionParagraph do
use Ash.Resource,
data_layer: AshPostgres.DataLayer

postgres do
table "question_paragraphs"
repo One.Repo
end

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

attributes do
uuid_primary_key :id
attribute :ordinal, :integer
attribute :text, :string
attribute :image, :string
end

relationships do
belongs_to :question, One.Exam.Question
end

identities do
identity :order, [:ordinal, :question]
end
end
defmodule One.Exam.QuestionParagraph do
use Ash.Resource,
data_layer: AshPostgres.DataLayer

postgres do
table "question_paragraphs"
repo One.Repo
end

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

attributes do
uuid_primary_key :id
attribute :ordinal, :integer
attribute :text, :string
attribute :image, :string
end

relationships do
belongs_to :question, One.Exam.Question
end

identities do
identity :order, [:ordinal, :question]
end
end
I want paragraphs belonging to a question to be ordered so they have an ordinal which needs to be unique, but only for that specific Question. How do I do that?
2 Replies
ZachDaniel
ZachDaniel2y ago
You'd want to use question_id in the identity which is automatically added to your resource by the question relationship
bryan
bryanOP2y ago
🤦‍♂️ Of course, thank you 🫡

Did you find this page helpful?