AE
Ash Elixir•3y ago
Blibs

Loading nested resource encrypted fields crashes ash_postgres

I have these resources:
defmodule Marketplace.Markets.Property.Offer do
...

relationships do
belongs_to :offeror, Offeror, allow_nil?: false
end
end

defmodule Marketplace.Markets.Property.Offeror do
@moduledoc false

alias Marketplace.Markets

use Ash.Resource,
data_layer: AshPostgres.DataLayer

attributes do
uuid_primary_key :id

attribute :email, :ci_string, allow_nil?: false

attribute :encrypted_phone_number, :binary, allow_nil?: false, private?: true
attribute :encrypted_first_name, :binary, allow_nil?: false, private?: true
attribute :encrypted_surname, :binary, allow_nil?: false, private?: true
end

calculations do
alias Marketplace.Ash.Calculations.Decrypt

calculate :phone_number, :string, {Decrypt, field: :encrypted_phone_number}
calculate :first_name, :string, {Decrypt, field: :encrypted_first_name}
calculate :surname, :string, {Decrypt, field: :encrypted_surname}
end

relationships do
has_one :offer, Markets.Property.Offer
end

postgres do
table "users"

repo Marketplace.Repo

migrate? false
end

actions do
defaults [:read]
end
end
defmodule Marketplace.Markets.Property.Offer do
...

relationships do
belongs_to :offeror, Offeror, allow_nil?: false
end
end

defmodule Marketplace.Markets.Property.Offeror do
@moduledoc false

alias Marketplace.Markets

use Ash.Resource,
data_layer: AshPostgres.DataLayer

attributes do
uuid_primary_key :id

attribute :email, :ci_string, allow_nil?: false

attribute :encrypted_phone_number, :binary, allow_nil?: false, private?: true
attribute :encrypted_first_name, :binary, allow_nil?: false, private?: true
attribute :encrypted_surname, :binary, allow_nil?: false, private?: true
end

calculations do
alias Marketplace.Ash.Calculations.Decrypt

calculate :phone_number, :string, {Decrypt, field: :encrypted_phone_number}
calculate :first_name, :string, {Decrypt, field: :encrypted_first_name}
calculate :surname, :string, {Decrypt, field: :encrypted_surname}
end

relationships do
has_one :offer, Markets.Property.Offer
end

postgres do
table "users"

repo Marketplace.Repo

migrate? false
end

actions do
defaults [:read]
end
end
When I fetch my offers and try to load the offeror name, ash_postgres will crash:
[offer] = Marketplace.Markets.read! Marketplace.Markets.Property.Offer
Marketplace.Markets.load(offer, [offeror: [:first_name, :surname]])
[offer] = Marketplace.Markets.read! Marketplace.Markets.Property.Offer
Marketplace.Markets.load(offer, [offeror: [:first_name, :surname]])
3 Replies
Blibs
BlibsOP•3y ago
Here is the error:
Blibs
BlibsOP•3y ago
If I try to load all the Offerors directly and load their name, it works fine, it just fails when I'm trying to load from an offer
ZachDaniel
ZachDaniel•3y ago
🤔 that is weird can I see your Decrypt calculation?

Did you find this page helpful?