Part 2: Database Schema

In this part, you'll design and create the database schema for your blog engine using CQL migrations. You'll create tables for users, categories, posts, and comments with proper relationships and indexes.

What You'll Learn

  • Designing a relational database schema

  • Creating migrations for multiple tables

  • Defining primary keys, foreign keys, and indexes

  • Using timestamps and default values

  • Running and verifying migrations

Prerequisites

Database Design

Here's the schema we'll create:

USERS                    CATEGORIES
├── id (PK)              ├── id (PK)
├── username (unique)    ├── name
├── email (unique)       ├── slug (unique)
├── first_name           ├── created_at
├── last_name            └── updated_at
├── active
├── created_at
└── updated_at

POSTS                    COMMENTS
├── id (PK)              ├── id (PK)
├── title                ├── content
├── content              ├── post_id (FK)
├── published            ├── user_id (FK, nullable)
├── views_count          ├── created_at
├── user_id (FK)         └── updated_at
├── category_id (FK)
├── created_at
└── updated_at

Step 1: Create Users Migration

Create the first migration for the users table:

Key points:

  • primary :id creates an auto-incrementing primary key

  • null: false makes columns required

  • default: true sets a default value

  • timestamps creates created_at and updated_at columns

  • Unique indexes on email and username prevent duplicates

Step 2: Create Categories Migration

The slug column stores URL-friendly versions of category names (e.g., "Web Development" becomes "web-development").

Step 3: Create Posts Migration

Key points:

  • Foreign keys establish relationships with users and categories

  • on_delete: :cascade deletes posts when the author is deleted

  • on_delete: :set_null keeps posts when a category is deleted (just nullifies the reference)

  • Indexes on foreign keys improve query performance

  • Index on published helps filter posts efficiently

Step 4: Create Comments Migration

Key points:

  • user_id is nullable to allow anonymous comments

  • Cascading delete removes comments when their post is deleted

  • Setting user to NULL when a user is deleted preserves the comment

Step 5: Run the Migrations

Update your setup script to require migrations:

Run the migrations:

Expected output:

Step 6: Verify the Schema

Create a verification script to inspect the created tables:

Run it:

Understanding the Schema

Let's visualize the relationships:

Migration Best Practices

  1. Number migrations sequentially: Use 001_, 002_, etc. to ensure order

  2. Write reversible migrations: Always implement both up and down

  3. One concern per migration: Don't create multiple unrelated tables in one migration

  4. Index foreign keys: Always index columns used in foreign key relationships

  5. Plan for deletion: Choose appropriate on_delete behavior

  6. Use nullable carefully: Only make columns nullable when truly optional

Common Column Types

CQL Type
SQLite
PostgreSQL
MySQL

primary

INTEGER

BIGSERIAL

BIGINT

bigint

INTEGER

BIGINT

BIGINT

integer

INTEGER

INTEGER

INT

text

TEXT

TEXT

TEXT

boolean

INTEGER

BOOLEAN

TINYINT

timestamps

TEXT (x2)

TIMESTAMP (x2)

DATETIME (x2)

Summary

In this part, you:

  1. Designed a relational database schema for a blog

  2. Created four migrations: users, categories, posts, comments

  3. Defined primary keys, foreign keys, and indexes

  4. Ran migrations to create the database tables

  5. Verified the schema was created correctly

Next Steps

In Part 3: Models and Relationships, you'll create Active Record models that map to these tables and define the relationships between them.


Tutorial Navigation:

Last updated

Was this helpful?