-
-
Notifications
You must be signed in to change notification settings - Fork 27
How to relationship
MasterDuke17 edited this page Sep 7, 2019
·
2 revisions
The way to represent a foreign key is using is referencing that has the same meaning as is column{ :references(...) }
A relationship is expressed with the trait is relationship
and it's not a column. A relationship must receive a reference to a foreign key (a column with is referencing
) (it will change in the future), and should pass a block that will return it.
If the attribute is Scalar
(has $.attr is relationship{...}
) the block will receive it's own model as a parameter to return the foreign key.
If the attribute is Positional
(has @.attr is relationship{...}
) the block will receive the target model as a parameter to return the foreign key.
model Person {...}
model Post is rw {
has Int $.id is serial;
has Str $.title is column{ :unique };
has Str $.body is column;
has Int $!author-id is referencing{ Person.id };
has Person $.author is relationship{ .author-id };
}
model Person is rw {
has Int $.id is serial;
has Str $.name is column;
has Post @.posts is relationship{ .author-id };
}
or
model Post is rw {
has Int $.id is serial;
has Str $.title is column{ :unique };
has Str $.body is column;
has Int $!author-id is referencing{ :model<Person>, :column<id> };
has $.author is relationship( *.author-id, :model<Person> );
}
model Person is rw {
has Int $.id is serial;
has Str $.name is column;
has @.posts is relationship( *.author-id, :model<Post> );
}