Ash FrameworkAF
Ash Framework3y ago
9 replies
waseigo

Example of setting a has_one relationship

I would appreciate an example of setting up a has_one relationship.

I have the following resources:

App.Api.Item
App.Api.Variant


Each resource has an attribute uuid_primary_key :id.

I want to establish a 1:1 relationship between the two resources.

So, I add the following to App.Api.Item:

relationships do
    has_one :variant, App.Api.Variant
end


I get the following errors/warnings:

warning: invalid association `variant` in schema App.Api.Item: associated schema App.Api.Variant does not have field `item_id`
  lib/app/api/resources/item.ex:1: App.Api.Item (module)

** (EXIT from #PID<0.96.0>) an exception was raised:
    ** (Spark.Error.DslError) [App.Api.Item]
 relationships -> variant:
  Relationship `variant` expects destination field `item_id` to be defined on App.Api.Variant


Do I need to define an attribute :item_id in resource App.Api.Variant? If so, what type would it be?

Or am I on the wrong path?
Was this page helpful?