HasOne
In this guide, we'll cover the HasOne
relationship using CQL's Active Record syntax. Like in the previous BelongsTo
guide, we’ll start with an Entity-Relationship Diagram (ERD) to visually represent how the HasOne
relationship works and build on the structure we already introduced with the BelongsTo
relationship.
What is a HasOne
Relationship?
HasOne
Relationship?The HasOne
relationship indicates that one entity (a record) is related to exactly one other entity. For example, a User can have one Profile associated with it. This relationship is a one-to-one mapping between two entities.
Example Scenario: Users and Profiles
Let's say we have a system where:
A User can have one Profile.
A Profile belongs to one User.
We will represent this one-to-one relationship using CQL’s HasOne
and BelongsTo
associations.
Defining the Schema
We'll define the users
and profiles
tables in the schema using CQL.
users table: Stores user details like
name
andemail
.profiles table: Stores profile details like
bio
andavatar_url
. It has auser_id
foreign key referencing theusers
table.
Defining the Models
Let’s define the User
and Profile
models in CQL, establishing the HasOne
and BelongsTo
relationships.
User Model
The
has_one :profile
association in theUser
model indicates that each user has one profile.
Profile Model
The
belongs_to :user
association in theProfile
model links each profile to a user by itsuser_id
.
Creating and Querying Records
Now that we have define the User
and Profile
models with a has_one
and belongs_to
relationship, let's see how to create and query records in CQL.
Creating a User and Profile
First, we create a
User
and save it to the database.Then, we create a
Profile
and associate it with the user by passinguser.id
as theuser_id
.
Accessing the Profile from the User
Once a user and their profile have been created, you can retrieve the profile using the has_one
association.
Here, user.profile
fetches the profile associated with the user.
Accessing the User from the Profile
Similarly, you can retrieve the associated user from the profile.
In this example, profile.user
fetches the User
associated with that Profile
.
Updating and Deleting the Associations
Updating the Profile for a User
You can update the profile associated with a user in the same way you would update any other record.
Here, we retrieve the profile associated with the user, modify its bio
, and save the changes.
Deleting the Profile
You can also delete the associated profile, but note that this does not automatically delete the user.
Similarly, deleting the user will not automatically delete the associated profile unless cascade rules are explicitly set in the database.
Summary
In this guide, we explored the has_one
relationship in CQL. We:
Define the
User
andProfile
tables in the schema.Created corresponding models, specifying the
has_one
relationship in theUser
model and thebelongs_to
relationship in theProfile
model.Demonstrated how to create, query, update, and delete records using the
has_one
andbelongs_to
associations.
Next Steps
In the next guide, we’ll extend the ERD and cover the has_many
relationship, which is commonly used when one entity is associated with multiple records (e.g., a post having many comments).
Feel free to experiment with the has_one
relationship by adding more fields to your models, setting up validations, or extending your schema with more complex relationships.
Last updated