Relationship expects Destination Field

I'm sure this is so basic but I keep running into this when adding relationships. I haven't tried anything very complicated.. just adding simple has_one relationships. I keep getting:
[{
"message": "an exception was raised:\n ** (Spark.Error.DslError) [FlowerShopApp.Accounts.Staff]\n relationships -> person:\n Relationship `person` expects destination field `staff_id` to be defined on FlowerShopApp.Accounts.Person\n (ash 2.5.16)
etc...
[{
"message": "an exception was raised:\n ** (Spark.Error.DslError) [FlowerShopApp.Accounts.Staff]\n relationships -> person:\n Relationship `person` expects destination field `staff_id` to be defined on FlowerShopApp.Accounts.Person\n (ash 2.5.16)
etc...
Here is my staff relationships:
relationships do
has_one :person, FlowerShopApp.Accounts.Person
end
relationships do
has_one :person, FlowerShopApp.Accounts.Person
end
And my Person attributes:
attributes do
uuid_primary_key :id

attribute :first_name, :string
attribute :last_name, :string
attribute :phone, :string
attribute :address_1, :string
attribute :address_2, :string
attribute :city, :string
attribute :state, :string
attribute :zip, :string
attribute :country, :string
end
attributes do
uuid_primary_key :id

attribute :first_name, :string
attribute :last_name, :string
attribute :phone, :string
attribute :address_1, :string
attribute :address_2, :string
attribute :city, :string
attribute :state, :string
attribute :zip, :string
attribute :country, :string
end
No person relationships ATM. What am I missing?
3 Replies
ZachDaniel
ZachDaniel•3y ago
So all relationships (many to many is a bit more involved) have a source_attribute and a destination_attribute We assume default values for those For belongs_to :foo, ... we assume the source_attribute is :foo_id and the destination_attribute is :id. For has_one we assume that the destination_attribute is the last part of the source module name, snake cased, followed by id. So the resource you're showing must be called something like FlowerShopApp.Accounts.Staff, and so we assume staff_id. And for has_one we assume the source_attribute is :id. The way its currently set up, it has no way of knowing which staff record belongs to a person record. Do you perhaps want a belongs_to :person, FlowerShopApp.Accounts.Person? For belongs_to relationships, we both assume that the source_attribute is :person_id and we define it automatically by default.
lifeling
lifelingOP•3y ago
Thanks. I have switched to belongs_to for these which works for me. I think I'm still not quite sure how has_one and belongs_to differ. But I'm thinking that I need to look at expanding my database and SQL knowledge. Thanks for pointing me in the right direction.
ZachDaniel
ZachDaniel•3y ago
👍 generally speaking “belongs to” means “I have a field that points to you” and “has one” means “you have a field that points to me. If you were to change who a record “belongs to” you’d change a field on this record So that’s why we define the attribute when using belongs_to

Did you find this page helpful?