Users and projects
Migrations describe your tables, models read and write their rows, and seeders fill them with sample data. By the end of this lesson your database holds three users and two projects, and two commands rebuild it.
1. Create the Migrations
A migration is a Python file that creates or changes a table. artisan writes the file for you:
uv run python artisan db:make:migration create_users_table --create users
uv run python artisan db:make:migration create_projects_table --create projectsEach command prints the file it created, such as databases/migrations/2026_11_06_115913_create_users_table.py. The date and time at the front keep migrations in order, so yours will differ.
Fill in the users migration:
from fastapi_startkit.masoniteorm import Migration
class CreateUsersTable(Migration):
async def up(self):
async with await self.schema.create("users") as table:
table.increments("id")
table.string("name")
table.string("email").unique()
table.timestamps()
async def down(self):
await self.schema.drop("users")And the projects migration:
from fastapi_startkit.masoniteorm import Migration
class CreateProjectsTable(Migration):
async def up(self):
async with await self.schema.create("projects") as table:
table.increments("id")
table.string("key", 10).unique()
table.string("name")
table.text("description").nullable()
table.timestamps()
async def down(self):
await self.schema.drop("projects")up() makes the change and down() undoes it. timestamps() adds created_at and updated_at columns, which the ORM fills in for you.
The key is the short code in front of every issue key, like WEB in WEB-1. unique() makes the database refuse two projects with the same key.
2. Add the Models
A model is a class for one table. You use it to read and write rows. Make one for each table:
uv run python artisan db:make:model User --directory app/models --pep
uv run python artisan db:make:model Project --directory app/models --pep--pep gives the files snake_case names, user.py and project.py. Open app/models/user.py and list its columns as annotated fields:
from fastapi_startkit.masoniteorm.models import Model
class User(Model):
pass
id: int
name: str
email: strDo the same in app/models/project.py:
from fastapi_startkit.masoniteorm.models import Model
class Project(Model):
pass
id: int
key: str
name: str
description: strThe fields do two jobs. They decide which columns you can fill when you create a row, and they convert values as they come out of the database.
You don't name the table. User uses users and Project uses projects, the plural of the class name.
3. Seed Some Data
A seeder is a class that puts rows in the database. Create one for each model:
uv run python artisan seed User
uv run python artisan seed ProjectFill in databases/seeders/user_table_seeder.py:
from fastapi_startkit.masoniteorm.seeders import Seeder
from app.models.user import User
class UserTableSeeder(Seeder):
async def run(self) -> None:
pass
await User.create({"name": "Ada Lovelace", "email": "ada@example.com"})
await User.create({"name": "Grace Hopper", "email": "grace@example.com"})
await User.create({"name": "Alan Turing", "email": "alan@example.com"}) And databases/seeders/project_table_seeder.py:
from fastapi_startkit.masoniteorm.seeders import Seeder
from app.models.project import Project
class ProjectTableSeeder(Seeder):
async def run(self) -> None:
pass
await Project.create({"key": "WEB", "name": "Website", "description": "The marketing site"})
await Project.create({"key": "APP", "name": "Mobile app"}) create() inserts a row and returns the model, with the id the database gave it.
db:seed starts from one seeder called DatabaseSeeder, which calls the others. Call both from databases/seeders/database_seeder.py:
from fastapi_startkit.masoniteorm.seeders import Seeder
from databases.seeders.project_table_seeder import ProjectTableSeeder
from databases.seeders.user_table_seeder import UserTableSeeder
class DatabaseSeeder(Seeder):
async def run(self):
pass
await self.call(UserTableSeeder, ProjectTableSeeder) When you add more seeders later, pass them to the same call().
Check
Create the tables and seed them:
uv run python artisan db:migrate
uv run python artisan db:seedMigrating: 2026_11_06_115913_create_users_table
Migrated: 2026_11_06_115913_create_users_table (0.00s)
Migrating: 2026_11_06_115914_create_projects_table
Migrated: 2026_11_06_115914_create_projects_table (0.00s)
Database Seeder seeded!Ask which migrations have run:
uv run python artisan db:migrate:statusBoth are listed with Y in the Ran? column. If you made a mistake in a migration, undo the last batch with db:migrate:rollback, fix the file and run db:migrate again.
If you have the sqlite3 command, look inside:
sqlite3 databases/database.sqlite "select id, key, name from projects"1|WEB|Website
2|APP|Mobile appStart again any time
uv run python artisan db:migrate:fresh drops every table and runs all migrations again. Follow it with db:seed to get the sample data back the way it started.
Compare your code
This lesson's code is tagged lesson-05. See the changes in this lesson.