relationship

Relationships

Relationships in Red are defined by the is relationship trait. It should receive (at least) a Str named parameter called :model that represents the related model, a Callable positional parameter that will return one (or more) referencing columns, and it must have a sigil. The sigil can be:

  • $ - Represents a belongs to relationship that "stores" only 1 element. Red will run the callable passing the model itself as the only parameter.

  • @ - Represents a has many relationship that "stores" a ResultSeq of elements. Red will run the callable passing the model defined by the :model named parameter.

(for the experimental has-one, please take a look at experimental doc)

So, for example:

# Person.rakumod
use Red:ver<2>;

model Person {
  has UInt $!id    is serial;
  has Str  $.name  is column;
  has      @.books is relationship(*.author-id, :model<Book>);
}
# Book.rakumod
use Red:ver<2>;

model Book {
  has UInt $!id        is serial;
  has Str  $.name      is column;
  has UInt $!author-id is referencing(*.id, :model<Person>);
  has      $.author    is relationship(*.author-id, :model<Person>);
}

Book has a foreign key (.author) that's auto pre-fetched using a join based on book.author_id = person.id.

Person has no foreign key but it does have a relationship (@.books) that isn't pre-fetched because it's a has many relationship. So when its value is accessed a new query is run.

On that example, to create a new book related to (written by) a given person, one could do something like:

my $author = Person.^all.first: *.name eq "Chico Buarque";
$author.books.create: :name<Benjamin>;

As mentioned, @.books is a ResultSeq, so it accepts any of its methods, eg:

$author.books.grep(*.starts-with: "Ben").elems

For getting the number of books written by "Chico Buarque" that starts with "Ben".

Red v0.1.55

A Raku ORM

Authors

  • Fernando Correa de Oliveira

License

Artistic-2.0

Dependencies

DBIishDB::PgUUID

Test Dependencies

Provides

  • CX::Red::Bool
  • MetamodelX::Red::Comparate
  • MetamodelX::Red::Describable
  • MetamodelX::Red::Dirtable
  • MetamodelX::Red::Id
  • MetamodelX::Red::Migration
  • MetamodelX::Red::Model
  • MetamodelX::Red::OnDB
  • MetamodelX::Red::Populatable
  • MetamodelX::Red::Refreshable
  • MetamodelX::Red::Relationship
  • MetamodelX::Red::Specialisable
  • MetamodelX::Red::Supply
  • Red
  • Red::AST
  • Red::AST::AddForeignKeyOnTable
  • Red::AST::BeginTransaction
  • Red::AST::Case
  • Red::AST::Chained
  • Red::AST::ChangeColumn
  • Red::AST::Comment
  • Red::AST::CommitTransaction
  • Red::AST::Constraints
  • Red::AST::CreateColumn
  • Red::AST::CreateTable
  • Red::AST::DateTimeFuncs
  • Red::AST::Delete
  • Red::AST::Divisable
  • Red::AST::DropColumn
  • Red::AST::Empty
  • Red::AST::Function
  • Red::AST::Generic::Infix
  • Red::AST::Generic::Postfix
  • Red::AST::Generic::Prefix
  • Red::AST::Infix
  • Red::AST::Infixes
  • Red::AST::Insert
  • Red::AST::Intersect
  • Red::AST::IsDefined
  • Red::AST::JsonItem
  • Red::AST::JsonRemoveItem
  • Red::AST::LastInsertedRow
  • Red::AST::Minus
  • Red::AST::MultiSelect
  • Red::AST::Next
  • Red::AST::Operator
  • Red::AST::Optimizer::AND
  • Red::AST::Optimizer::Case
  • Red::AST::Optimizer::OR
  • Red::AST::RollbackTransaction
  • Red::AST::Select
  • Red::AST::StringFuncs
  • Red::AST::TableComment
  • Red::AST::Unary
  • Red::AST::Union
  • Red::AST::Update
  • Red::AST::Value
  • Red::Attr::Column
  • Red::Attr::Query
  • Red::Attr::Relationship
  • Red::Class
  • Red::Cli
  • Red::Cli::Column
  • Red::Cli::Relationship
  • Red::Cli::Table
  • Red::Column
  • Red::ColumnMethods
  • Red::Config
  • Red::DB
  • Red::Database
  • Red::DefaultResultSeq
  • Red::Do
  • Red::Driver
  • Red::Driver::Cache
  • Red::Driver::Cache::Memory
  • Red::Driver::CacheInvalidateOnWrite
  • Red::Driver::CacheWithStrKey
  • Red::Driver::CommonSQL
  • Red::Driver::Mock
  • Red::Driver::Pg
  • Red::Driver::SQLite
  • Red::Driver::SQLite::SQLiteMaster
  • Red::Driver::SQLite::SchemaReader
  • Red::Event
  • Red::Formatter
  • Red::FromRelationship
  • Red::HiddenFromSQLCommenting
  • Red::Migration::Column
  • Red::Migration::Migration
  • Red::Migration::Table
  • Red::Model
  • Red::Operators
  • Red::Phaser
  • Red::PrepareCode
  • Red::ResultAssociative
  • Red::ResultSeq
  • Red::ResultSeq::Iterator
  • Red::ResultSeqSeq
  • Red::Schema
  • Red::SchemaReader
  • Red::Statement
  • Red::Traits
  • Red::Type
  • Red::Type::Json
  • Red::Utils
  • X::Red::Exceptions

Documentation

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.