Skip to contents

This function establishes a relationship between two models in the ORM system. It creates a Relationship object and adds it to the local model's relationships. If a backref is specified, it also creates a reverse relationship in the related model. As this uses R6 classes, the models are modified in place and do not need to be reassigned.

Usage

define_relationship(
  local_model,
  local_key,
  type = c("one_to_one", "one_to_many", "many_to_one", "many_to_many"),
  related_model,
  related_key,
  ref = NULL,
  backref = NULL
)

Arguments

local_model

The model that owns the relationship.

local_key

The key in the local model that relates to the foreign key in the related model.

type

The type of relationship. Must be one of 'one_to_one', 'one_to_many', 'many_to_one', or 'many_to_many'.

The model that is being related to.

The key in the related model that the local_key relates to.

ref

The name to use for this relationship in the local model. If NULL, defaults to the lowercase name of the related model.

backref

The name to use for the reverse relationship in the related model. If NULL, defaults to the lowercase name of the local model. If FALSE, no reverse relationship is created.

Value

Invisibly returns the local_model (although the model is modified in place).

Examples

if (FALSE) { # \dontrun{
# Set up models
User <- engine$model("users", id = Column("INTEGER", primary_key = TRUE))
Post <- engine$model("posts", 
                     id = Column("INTEGER", primary_key = TRUE), 
                     user_id = Column("INTEGER"))

# Define a one-to-many relationship from User to Post
define_relationship(User, "id", "one_to_many", Post, "user_id", 
                    ref = "posts", backref = "user")

# Now User has a 'posts' relationship and Post has a 'user' relationship

# Create some sample data
user <- User$record(id = 1)$create()
post <- Post$record(id = 1, user_id = 1)$create()

# Use the relationships
user_posts <- user$relationship('posts')
post_user <- post$relationship('user')

print(user_posts)  # Should show the post
print(post_user)   # Should show the user
} # }