Compile error: module Any is reserved and cannot be defined
Say I have the following resources:
City:
State:
Region:
Credo seems to complain about state, saying:
Not really sure what to do about that.
13 Replies
Hey there. So it's because the relationships expect the related schemas to have an ID field called
:id
- so for example in your has_one :state State
in City
you need to add destination_attribute
. See the DSL docs for more information: https://ash-hq.org/docs/dsl/ash-resource#relationships-has_one-destination_attributeI'd use belongs_to instead of has_one in this example (has_one puts the foreign key on the other table)
right but you need to set
source_attribute
and destination_attribute
appropriately so that ash knows which fields contain the foreign keysYep, absolutely
Hmm I'm pretty sure I did that earlier and it still complained. Let me add it in again.
This works:
So for when you want foreign keys,
belongs_to
is what's preferable?Yeah, I think of has_one as being like has_many (you'd use on the other side of the relationship)
Oh so
has_one
would be in the region resource, not the state resource?also, you could use
uuid_primary_key :id
on all those resources, and not need to provide the source and destination attributes to belongs_to
Yep, you could but i think you'll want has_many
ie, a region has many states
i might be misunderstanding the relationship thoughOkay, makes sense. I'm more or less trying to replicate something for work, because in the future we're considering moving away from our Java codebase and I'd like to have something to propose. So maybe
has_many
might work, considering a state has a region attribute that has the Java annotation @ManyToOne
.yep, so you'd have
has_many
on one side, and belongs_to
on the other. If you've got a @OneToOne
, you'll want a belongs_to
on one side (the side with the foreign key) and a has_one
on the otherAh, now it makes more sense.
I think of
belongs_to
as defining the relationship (it sets the foreign key on that resource) and has_one
and has_many
as utilities to go 'the other way' (if required) if that makes senseMakes sense.