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: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’sSTRICT 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
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 Naming Convention
Follow TrailBase’s naming convention for consistency:_articles__author_index_todos__user_created_index
Constraints
Foreign Keys
Enforce referential integrity with foreign keys:ON DELETE CASCADE- Delete child records when parent is deletedON DELETE SET NULL- Set foreign key to NULL when parent is deletedON 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
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:Virtual Tables
TrailBase supports virtual tables for special use cases:FTS5 Full-Text Search
Custom Query APIs
Optimization Tips
1
Use Appropriate Types
Choose the smallest type that fits your data:
- Use
INTEGERfor IDs and counts (notTEXT) - Use
INTEGERfor timestamps (UNIX epoch) - Avoid
ANYtype 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_):
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