Skip to content

Working Relationship

Fernando Correa de Oliveira edited this page Sep 25, 2018 · 1 revision
use Red;

model Bla {
   has Int $.id is id;
   has Str $.name is column;
}
model Ble {
   has Int $.id is id;
   has Int $.bla-id is referencing{ Bla.id };
   has Bla $.bla is relationship{ .bla-id }; 
}
my $*RED-DEBUG = True;

my $*RED-DB = database "SQLite";

Bla.^create-table;
Ble.^create-table;

Bla.^create: :1id, :name<test>;
say Ble.^create(:1id, :1bla-id).bla;

Output:

SQL : CREATE TABLE bla(
   name varchar(255) NULL,
   id integer NOT NULL primary key
)
SQL : CREATE TABLE ble(
   id integer NOT NULL primary key,
   bla_id integer NULL
)
SQL : INSERT INTO bla(
   id,
   name
)
VALUES(
   1,
   'test'
)
SQL : INSERT INTO ble(
   id,
   bla_id
)
VALUES(
   1,
   1
)
SQL : SELECT
   name as name, id as id
FROM
   bla
WHERE
   id = 1
LIMIT 1
Bla.new(id => 1, name => "test")
Clone this wiki locally