manage_relationship not creating many-to-many join records

I'm having trouble getting manage_relationship to work with a many-to-many relationship where the destination resource uses a non-standard primary key. Setup I have a many-to-many relationship between Device and Pass through a DeviceRegistration join table: - Device: Standard resource with uuid_primary_key :id - Pass: Uses uuid_primary_key :serial_number (not :id) - DeviceRegistration: Join table with device_id and serial_number fields Resource Configurations Device Resource
defmodule MyApp.Device do
use Ash.Resource

actions do
create :register do
primary? true
accept [:apple_device_id, :push_token]
argument :passes, {:array, :uuid}, allow_nil?: true

change manage_relationship(:passes, type: :append, value_is_key: :serial_number)

upsert? true
upsert_identity :unique_device_id
end
end

relationships do
many_to_many :passes, MyApp.Pass do
through MyApp.DeviceRegistration
source_attribute_on_join_resource :device_id
destination_attribute :serial_number
destination_attribute_on_join_resource :serial_number
end
end
end
defmodule MyApp.Device do
use Ash.Resource

actions do
create :register do
primary? true
accept [:apple_device_id, :push_token]
argument :passes, {:array, :uuid}, allow_nil?: true

change manage_relationship(:passes, type: :append, value_is_key: :serial_number)

upsert? true
upsert_identity :unique_device_id
end
end

relationships do
many_to_many :passes, MyApp.Pass do
through MyApp.DeviceRegistration
source_attribute_on_join_resource :device_id
destination_attribute :serial_number
destination_attribute_on_join_resource :serial_number
end
end
end
3 Replies
ajst7les
ajst7lesOP3w ago
Pass Resource
defmodule MyApp.Pass do
use Ash.Resource

attributes do
uuid_primary_key :serial_number # <-- Non-standard primary key
# ... other attributes
end

relationships do
many_to_many :devices, MyApp.Device do
through MyApp.DeviceRegistration
source_attribute_on_join_resource :serial_number
destination_attribute :id
destination_attribute_on_join_resource :device_id
end
end
end
defmodule MyApp.Pass do
use Ash.Resource

attributes do
uuid_primary_key :serial_number # <-- Non-standard primary key
# ... other attributes
end

relationships do
many_to_many :devices, MyApp.Device do
through MyApp.DeviceRegistration
source_attribute_on_join_resource :serial_number
destination_attribute :id
destination_attribute_on_join_resource :device_id
end
end
end
DeviceRegistration Join Resource
defmodule MyApp.DeviceRegistration do
use Ash.Resource

actions do
create :create do
primary? true
accept [:device_id, :serial_number]
end
end

attributes do
uuid_primary_key :id
timestamps()
end

relationships do
belongs_to :device, MyApp.Device do
allow_nil? false
end

belongs_to :pass, MyApp.Pass do
source_attribute :serial_number
destination_attribute :serial_number
allow_nil? false
end
end

identities do
identity :unique_registration, [:device_id, :serial_number]
end
end
defmodule MyApp.DeviceRegistration do
use Ash.Resource

actions do
create :create do
primary? true
accept [:device_id, :serial_number]
end
end

attributes do
uuid_primary_key :id
timestamps()
end

relationships do
belongs_to :device, MyApp.Device do
allow_nil? false
end

belongs_to :pass, MyApp.Pass do
source_attribute :serial_number
destination_attribute :serial_number
allow_nil? false
end
end

identities do
identity :unique_registration, [:device_id, :serial_number]
end
end
The Problem When I try to create a device with passes:
{:ok, device} = Ash.create(MyApp.Device, %{
apple_device_id: "device123",
push_token: "token123"
}, passes: [pass.serial_number])
{:ok, device} = Ash.create(MyApp.Device, %{
apple_device_id: "device123",
push_token: "token123"
}, passes: [pass.serial_number])
The device gets created successfully, but no join records are created in the DeviceRegistration table. When I load the relationship:
loaded_device = Ash.load!(device, :passes)
assert length(loaded_device.passes) == 1 # FAILS - passes is []
loaded_device = Ash.load!(device, :passes)
assert length(loaded_device.passes) == 1 # FAILS - passes is []
ZachDaniel
ZachDaniel3w ago
change manage_relationship(:passes, type: :append, value_is_key: :serial_number, debug?: true)
change manage_relationship(:passes, type: :append, value_is_key: :serial_number, debug?: true)
try adding debug? true and seeing what is logged Are you sure that no join records are created? i.e Ash.count!(JoinResource) == 0?
ajst7les
ajst7lesOP3w ago
Thanks I solved it with debug was a silly typo in the execution flow on my side that went unnoticed!!! Thanks for being so responsive!

Did you find this page helpful?