Has One
Last updated
Last updated
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.
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.
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.
We'll define the users
and profiles
tables in the schema using CQL.
users table: Stores user details like name
and email
.
profiles table: Stores profile details like bio
and avatar_url
. It has a user_id
foreign key referencing the users
table.
Let’s define the User
and Profile
models in CQL, establishing the HasOne
and BelongsTo
relationships.
The has_one :profile
association in the User
model indicates that each user has one profile.
The belongs_to :user
association in the Profile
model links each profile to a user by its user_id
.
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.
First, we create a User
and save it to the database.
Then, we create a Profile
and associate it with the user by passing user.id
as the user_id
.
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.
Similarly, you can retrieve the associated user from the profile.
In this example, profile.user
fetches the User
associated with that Profile
.
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.
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.
In this guide, we explored the has_one
relationship in CQL. We:
Define the User
and Profile
tables in the schema.
Created corresponding models, specifying the has_one
relationship in the User
model and the belongs_to
relationship in the Profile
model.
Demonstrated how to create, query, update, and delete records using the has_one
and belongs_to
associations.
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.