-
-
Notifications
You must be signed in to change notification settings - Fork 27
Another working relationship sample
Fernando Correa de Oliveira edited this page Sep 26, 2018
·
11 revisions
use Red;
model Person {...}
model Post {
has Int $.id is id;
has Int $!author-id is referencing{ Person.id };
has Str $.title is column{ :unique };
has Str $.body is column;
has Person $.author is relationship{ .author-id };
has Bool $.deleted is column = False;
}
model Person {
has Int $.id is id;
has Str $.name is column;
has Post @.posts is relationship{ .author-id };
}
my $*RED-DEBUG = True;
my $*RED-DB = database "SQLite";
Person.^create-table;
Post.^create-table;
my $p = Person.^create: :1id, :name<Fernando>;
$p.posts.create: :title<Bla>, :body<BlaBle1>;
$p.posts.create: :title<Ble>, :body<BlaBle2>;
say $p.posts.map: *.title
SQL : CREATE TABLE person(
name varchar(255) NULL,
id integer NOT NULL primary key
)
SQL : CREATE TABLE post(
author_id integer NULL,
body varchar(255) NULL,
deleted integer NULL,
id integer NOT NULL primary key,
title varchar(255) NULL
)
SQL : INSERT INTO person(
name,
id
)
VALUES(
'Fernando',
1
)
SQL : INSERT INTO post(
id,
deleted,
body,
title,
author_id
)
VALUES(
1,
False,
'Blable1',
'Bla',
1
)
SQL : INSERT INTO post(
id,
deleted,
body,
title,
author_id
)
VALUES(
2,
False,
'Blable2',
'Ble',
1
)
SQL : SELECT
title as "data"
FROM
post
WHERE
author_id = 1
(Bla Ble)