Identities split into multiple resources doesn't seem to work

Not sure if I'm doing something wrong, but I have 2 resources that uses the same table, User and Customer. In my User resource I have these identities:
identities do
identity :unique_email, [:email] do
eager_check_with FeedbackCupcake.Accounts
end

identity :unique_referral_code, [:referral_code]
end
identities do
identity :unique_email, [:email] do
eager_check_with FeedbackCupcake.Accounts
end

identity :unique_referral_code, [:referral_code]
end
And in my Customer resource, I have these identities:
identities do
identity :unique_customer_id, [:customer_id]
end
identities do
identity :unique_customer_id, [:customer_id]
end
For some reason, Ash just adds the indexes for the User resource in my PostgreSQL table, the one in Customer is simply ignored and doesn't show up in the migrations at all.
9 Replies
kernel
kernel2y ago
all the usual stuff i.e: added to the registry? api added to the ash_apis key in config?
ZachDaniel
ZachDaniel2y ago
Does customer have a customer_id attribute? Perhaps you mean :id?
Blibs
BlibsOP2y ago
Yes, the resource is added to the registry and the api is added to ash_apis, it is not that the resource itself is being ignored, all of its changes (new attributes, etc) are being correctly applied in migrations, just the identities that are not. Yes, the Customer has a customer_id attribute, User does not:
attribute :customer_id, :string do
constraints max_length: 255
end
attribute :customer_id, :string do
constraints max_length: 255
end
Not sure if this helps, but this is the Customer resource code:
defmodule FeedbackCupcake.Payments.Customer do
@moduledoc false

use Ash.Resource,
data_layer: AshPostgres.DataLayer

code_interface do
...
end

attributes do
alias FeedbackCupcake.Payments.Customer.PaymentMethod

alias FeedbackCupcake.Ash.Types.Currency

uuid_primary_key :id

attribute :email, :ci_string do
allow_nil? false

writable? false
end

attribute :first_name, :string do
allow_nil? false

writable? false
end

attribute :surname, :string do
allow_nil? false

writable? false
end

attribute :customer_id, :string do
constraints max_length: 255
end

attribute :payment_method, PaymentMethod

attribute :balance, Currency, allow_nil?: false, default: 0

timestamps()
end

relationships do
alias FeedbackCupcake.Payments.Subscription

has_one :subscription, Subscription
end

calculations do
calculate :full_name, :string, expr(string_join([first_name, surname], " "))
end

preparations do
prepare build(load: [:full_name, :subscription])
end

postgres do
table "users"

migration_ignore_attributes [:email, :first_name, :surname]

migration_types customer_id: {:varchar, 255}

repo FeedbackCupcake.Repo
end

identities do
identity :unique_customer_id, [:customer_id]
end

actions do
...
end
end
defmodule FeedbackCupcake.Payments.Customer do
@moduledoc false

use Ash.Resource,
data_layer: AshPostgres.DataLayer

code_interface do
...
end

attributes do
alias FeedbackCupcake.Payments.Customer.PaymentMethod

alias FeedbackCupcake.Ash.Types.Currency

uuid_primary_key :id

attribute :email, :ci_string do
allow_nil? false

writable? false
end

attribute :first_name, :string do
allow_nil? false

writable? false
end

attribute :surname, :string do
allow_nil? false

writable? false
end

attribute :customer_id, :string do
constraints max_length: 255
end

attribute :payment_method, PaymentMethod

attribute :balance, Currency, allow_nil?: false, default: 0

timestamps()
end

relationships do
alias FeedbackCupcake.Payments.Subscription

has_one :subscription, Subscription
end

calculations do
calculate :full_name, :string, expr(string_join([first_name, surname], " "))
end

preparations do
prepare build(load: [:full_name, :subscription])
end

postgres do
table "users"

migration_ignore_attributes [:email, :first_name, :surname]

migration_types customer_id: {:varchar, 255}

repo FeedbackCupcake.Repo
end

identities do
identity :unique_customer_id, [:customer_id]
end

actions do
...
end
end
ZachDaniel
ZachDaniel2y ago
Yeah, it’s a bug
ZachDaniel
ZachDaniel2y ago
GitHub
ash_postgres/lib/migration_generator/migration_generator.ex at main...
A postgresql datalayer for the Ash Framework. Contribute to ash-project/ash_postgres development by creating an account on GitHub.
ZachDaniel
ZachDaniel2y ago
That needs to add up all of the identities on all snapshots, just like it does for other things. I’m in the car, but if you want to PR a fix ill merge it
Blibs
BlibsOP2y ago
I will take a look
Blibs
BlibsOP2y ago
GitHub
fix: Fixes multiple schema identities migrations by sezaru · Pull R...
Contributor checklist Bug fixes include regression tests Features include unit/acceptance tests
Blibs
BlibsOP2y ago
done

Did you find this page helpful?