Active Record
In this guide, we'll walk through using CQL with Crystal for setting up a database schema, defining records (models), establishing relationships between them, and handling migrations
Getting Started with Active Record, Relations, and Migrations in CQL
In this guide, we'll walk through using CQL with Crystal for setting up a database schema, defining records (models), establishing relationships between them, and handling migrations. This will be a foundational guide for developers who are familiar with Object-Relational Mapping (ORM) concepts from other frameworks like ActiveRecord (Rails), Ecto, or Hibernate, but are now learning CQL with Crystal.
Prerequisites
Before getting started, ensure you have the following:
Crystal language installed (latest stable version).
PostgreSQL or MySQL set up locally or in the cloud.
CQL installed in your Crystal project.
You can add CQL to your project by including it in your shard.yml
:
Create the Record:
The
User
model is mapped to theusers
table. Here, we've define the fields forid
,name
,email
, and timestamps. We also added basic validations forname
andemail
.Create Records: You can now create user records using the
User
model.This will insert a new record into the
users
table.Query Records: You can query users using the
User
model.User.all
fetches all users, andUser.find(1)
fetches the user with ID1
.
Establishing Relations
CQL supports associations similar to ActiveRecord, Ecto, and other ORMs. Let's define some common relationships such as has_many
and belongs_to
.
Example: Users and Posts
Migration for Posts:
Create a new migration for the
posts
table.Edit the migration to add the
posts
table, which has a foreign key to theusers
table:db_context the Post Model:
Now, let's define the
Post
record and establish the relationships.Here, the
Post
model includes a foreign keyuser_id
and define abelongs_to
association to theUser
model.db_context the
User
model's association:Update the
User
model to reflect the relationship withPost
.This define a
has_many
association onUser
so that each user can have multiple posts.Working with Relations:
Create a user and associate posts with them:
Access posts through the user:
Handling Migrations
CQL migrations allow you to create and alter your database schema easily. Here are some common migration tasks:
Adding Columns:
If you need to add a new column to an existing table, generate a migration:
Update the migration to add the
age
column:Rolling Back Migrations:
If something goes wrong with a migration, you can roll it back using:
This will undo the last migration that was applied.
Conclusion
This guide has provided a basic overview of using CQL with Crystal to define records (models), create relationships, and handle migrations. You've learned how to:
Set up CQL and connect it to a database.
Create and run migrations to define your schema.
Define records and establish relationships using
has_many
andbelongs_to
.Manage your database schema with migrations.
With this foundation, you can now expand your models, add validations, and explore more advanced querying and relationships in CQL.
In the following guide, we'll take a closer look at the different relationships you can establish between models in CQL: BelongsTo
, HasOne
, HasMany
, and ManyToMany
. These relationships allow you to associate models with one another, making it easy to retrieve related data, enforce foreign key constraints, and maintain data integrity.
We'll use simple examples with CQL's DSL to help you understand how to define and use these associations effectively.
Last updated