Add a database table
Database changes are written as migrations: small .sql files that each run exactly once. Don't edit the schema files; they are the frozen 1.0 starting point.
Why migrations
Your laptop, your live site and a fresh install all need the same tables. If you add a table by hand on one of them, the others don't have it, and you find out when a page breaks. A migration file travels with your code, and the installer applies it wherever it hasn't run yet.
1. Write the file
One file per database you use, with the same name in each folder. SQLite and MySQL spell things differently, so they can't share a file.
database/migrations/sqlite/app-001-projects.sql
CREATE TABLE projects (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
title TEXT NOT NULL,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
);
CREATE INDEX idx_projects_user ON projects (user_id);
database/migrations/mysql/app-001-projects.sql
CREATE TABLE `projects` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT UNSIGNED NOT NULL,
`title` VARCHAR(120) NOT NULL,
`created_at` DATETIME NOT NULL,
`updated_at` DATETIME NOT NULL,
PRIMARY KEY (`id`),
KEY `idx_projects_user` (`user_id`),
CONSTRAINT `fk_projects_user` FOREIGN KEY (`user_id`)
REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
If you only ever use one database, you only need that folder.
2. Run it
SQLite: there is nothing to run. Load any signed-in page and the file is applied, on your own computer and on the live site. If the file has a mistake in it, nothing is changed: you get an error page, the reason is in storage/logs/app.log, and the next request tries again once the file is fixed.
MySQL:
php bin/install.php
The installer applies anything that hasn't been applied and records it in the migrations table. Running it again does nothing.
No shell access on a MySQL host? Import the file in phpMyAdmin, and end it with this line so the database remembers it ran:
INSERT INTO migrations (name, applied_at) VALUES ('app-001-projects', '2026-10-02 12:00:00');
Naming
Files run in alphabetical order, so the number sets the order.
| Prefix | Who writes it |
|---|---|
app- | You. app-001-projects.sql, app-002-invoices.sql. |
core- | Shipped with Core updates. Don't edit or rename. |
Three rules
- Every table that belongs to a user has
user_idwithON DELETE CASCADE. Deleting an account then deletes its data, which is what the user expects and what data-protection law requires. - Never change a migration after it has run anywhere. A database that already applied it will never run it again, so the edit would leave your live site and a fresh install with different tables. Write a new migration that corrects it.
- End every statement with a semicolon at the end of a line. The installer splits on that.
Then query it safely
Always scope queries to the signed-in user. This is the bug that shows one customer another customer's data:
// WRONG: any signed-in user can read any project by changing ?id=
db_one('SELECT * FROM projects WHERE id = ?', [$id]);
// RIGHT
db_one('SELECT * FROM projects WHERE id = ? AND user_id = ?', [$id, auth_id()]);
Build a feature puts a table, its queries and its pages together.