AE
Ash Elixirโ€ข2y ago
axdc

Identity not creating unique index

I have a resource with an identity:
attributes do
uuid_primary_key :id

attribute :title, :string do
allow_nil? false
end

attribute :slug, :string do
allow_nil? false
end

attribute :content, :string do
allow_nil? false
end

create_timestamp(:inserted_at)
update_timestamp(:updated_at)
end

relationships do
belongs_to :site, MyApp.Sites.Site
end

identities do
identity :slug_site, [:slug, :site]
end
attributes do
uuid_primary_key :id

attribute :title, :string do
allow_nil? false
end

attribute :slug, :string do
allow_nil? false
end

attribute :content, :string do
allow_nil? false
end

create_timestamp(:inserted_at)
update_timestamp(:updated_at)
end

relationships do
belongs_to :site, MyApp.Sites.Site
end

identities do
identity :slug_site, [:slug, :site]
end
When creating these resources, the uniqueness that I think I'm specifying is not being enforced, and I don't see a unique index in postgres reflecting it. I've dropped and recreated the database, deleting all migrations and snapshots to be sure. Do Identities not work with relationships? Is there a recommended way to communicate to Ash what I'm looking for? The intended behavior is that slugs should be unique within a site.
5 Replies
ZachDaniel
ZachDanielโ€ข2y ago
You want site_id, not site ๐Ÿ™‚ The generated attribute for the belongs_to :site is :site_id
frankdugan3
frankdugan3โ€ข2y ago
I wonder if that is something that could be caught in the validations and warned about. :thinkies: Because identities can only be based on attributes, right?
ZachDaniel
ZachDanielโ€ข2y ago
technically they can be used in an informative way on any non-relationship field. but that one is a relationship so yeah we could almost certainly just add a new verifier, i.e Ash.Resource.Verifiers.VerifyIdentityFields, would likely be trivial
frankdugan3
frankdugan3โ€ข2y ago
I won't get to it soon, but that might be something I (or another contributor) can tackle easily in a few minutes, so I'll create an issue for it.
frankdugan3
frankdugan3โ€ข2y ago
https://github.com/ash-project/ash/issues/576 Might want to mark it as a good first issue. ๐Ÿ™‚
GitHub
Validation: Ash.Resource.Verifiers.VerifyIdentityFields ยท Issue #57...
Is your feature request related to a problem? Please describe. Given this schema: attributes do uuid_primary_key :id attribute :title, :string do allow_nil? false end attribute :slug, :string do al...

Did you find this page helpful?