-
-
Notifications
You must be signed in to change notification settings - Fork 27
Another working relationship sample
Fernando Correa de Oliveira edited this page Sep 25, 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>; # NYI
Post.^create: :1id, :1author-id, :title<Bla>, :body<Blable1>;
# $p.posts.create: :title<Bl2>, :body<BlaBle2>; # NYI
Post.^create: :2id, :1author-id, :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)