Skip to main content
TrailBase uses SQLite as its database engine, providing a powerful yet simple foundation for your application. This guide covers schema design, table creation, and optimization strategies.

Schema Requirements

TrailBase requires tables to meet specific criteria to expose them via Record APIs:
1

STRICT Typing

All tables must use SQLite’s STRICT mode for type safety:
Without STRICT, SQLite allows any value in any column, which can lead to type errors in your application.
2

Primary Key Column

Tables must have an INTEGER or BLOB (for UUIDv7) primary key:
UUIDv7 provides globally unique, time-sortable identifiers. TrailBase provides built-in uuid_v7() and is_uuid_v7() functions.
3

Quote Column Names

Use double quotes around column names to avoid SQL keyword conflicts:

Column Types

SQLite’s STRICT mode enforces these types:
SQLite uses integers (0/1) for boolean values. TrailBase’s type generation will map these appropriately in client code.

Creating Tables

Basic Table Structure

Here’s a complete example from the blog example:
migrations/main/U1725019362__create_articles.sql
Key Features:
  • UUIDv7 primary key with validation
  • Foreign key to _user table with cascade delete
  • File upload column with JSON schema validation
  • Automatic timestamp using UNIXEPOCH()

Profiles Table with Username Validation

migrations/main/U1725019361__create_profiles.sql
TrailBase provides the REGEXP operator for pattern validation. Regular expressions are evaluated efficiently using Rust’s regex engine.

Indexes

Indexes dramatically improve query performance for large datasets:

Single-Column Indexes

Multi-Column Indexes

For queries that filter on multiple columns:
Index Order Matters: SQLite can only use the leftmost columns of an index. An index on (user, created) helps queries filtering by user alone, but not queries filtering only by created.

Index Naming Convention

Follow TrailBase’s naming convention for consistency:
Examples:
  • _articles__author_index
  • _todos__user_created_index

Constraints

Foreign Keys

Enforce referential integrity with foreign keys:
Cascade Actions:
  • ON DELETE CASCADE - Delete child records when parent is deleted
  • ON DELETE SET NULL - Set foreign key to NULL when parent is deleted
  • ON DELETE RESTRICT - Prevent parent deletion if children exist
TrailBase automatically enables foreign key enforcement (PRAGMA foreign_keys = ON).

Check Constraints

Validate data at the database level:

Unique Constraints

Default Values

Provide sensible defaults for optional fields:

Triggers

Automate common tasks with triggers:

Update Timestamp Trigger

Validation Trigger

Triggers run for every row operation and can impact performance. Use them judiciously.

Views

Views provide read-only, computed data combining multiple tables:

Views with Computed Columns

Type Casts Required: TrailBase generates JSON schemas from view column types. Use explicit CAST() for computed columns so TrailBase can infer the correct type.

File Upload Columns

Store file metadata with built-in validation:
The JSON schema validation ensures uploaded files meet your requirements. See the File Uploads guide for details.

Virtual Tables

TrailBase supports virtual tables for special use cases:

Custom Query APIs

Optimization Tips

1

Use Appropriate Types

Choose the smallest type that fits your data:
  • Use INTEGER for IDs and counts (not TEXT)
  • Use INTEGER for timestamps (UNIX epoch)
  • Avoid ANY type in STRICT tables
2

Index Foreign Keys

Always create indexes on foreign key columns:
3

Normalize Data

Avoid duplicating data across tables. Use foreign keys and joins instead:Bad:
Good:
4

Use Views for Complex Queries

Create views for frequently-used joins to simplify client code and ensure consistency.

Built-in Tables

TrailBase provides several built-in tables (prefixed with _):
Do not modify built-in tables directly. Use TrailBase’s auth APIs and admin UI instead.

Next Steps

Migrations

Learn how to evolve your schema over time

Authentication

Integrate user tables with auth

File Uploads

Add file upload columns

First App

Build a complete application