Skip to main content
GatoJS uses Prisma with PostgreSQL. This page covers the schema files, the client and connection pool, and how to run migrations in your project. For the data model and the organizationId rule, see Organizations.

Schema files

The schema is a directory, prisma/schema/, not a single file. Each module ships its own schema file, so adding a module never means editing core files. Back-relation fields on User and Organization are written by the kit’s generator into wa:relations regions. Don’t edit inside them by hand. npx gato generate rewrites them from installed modules:
prisma/schema/organizations.prisma
A typical domain model for your own feature looks like the kit’s modules: a required, indexed organizationId with a cascading relation.
prisma/schema/invoices.prisma
Because Invoice has an organizationId field, the runtime tenant-scope guard covers it automatically.

Configuration

Prisma CLI commands read prisma.config.ts:
prisma.config.ts
The database URL comes only from DATABASE_URL. A value in .env.local wins over .env, and a variable set in your shell wins over both. The app reads the same variable.

Client and connection

prisma generate writes the client to src/generated/prisma, which is git-ignored. It runs on postinstall, and manually with:
npx prisma migrate dev doesn’t run the generator in current Prisma versions. After any schema change, also run npm run db:generate, or TypeScript won’t know your new models and fields.
Import the client from @/generated/prisma/client for types, and use the shared instance for queries:
src/shared/lib/prisma.ts
  • Prisma connects through @prisma/adapter-pg with a pg pool of up to DATABASE_POOL_MAX connections (default 10 when missing or not a positive integer).
  • Idle connections close after 30 s, waiting for a free connection times out after 10 s, and Postgres cancels statements running longer than 30 s.
  • The client carries the tenant-scope extension.
  • The instance is cached on globalThis so next dev hot reloads reuse the pool.
Use Prisma only in commands, queries and services, never in components or pages. See Code patterns.

Migrations

The kit doesn’t commit migrations; your project does. A project’s schema depends on which modules you picked, since each module adds its own schema file and relations. A migration history shipped by the kit wouldn’t match a project with a different set of modules, so each project starts its own history with an init migration.
In the kit repository, prisma/migrations/ is ignored by a prisma/migrations/.gitignore that gato create doesn’t copy. In a project created with create-gato-app / gato create, prisma/migrations/ is not ignored, so migrations go into git with no extra steps. Production applies only migrations committed to the repo: the deploy runs npx prisma migrate deploy, which never creates migrations from the schema. Without a committed migrations directory, the production database gets no tables. See Railway and Docker.

Your first migration

If you let create-gato-app run setup, it already ran npx prisma migrate dev --name init. That happens after the initial commit, so the migration is still uncommitted. Otherwise, create it yourself:
1

Make sure migrations aren't ignored

This command prints nothing when the directory isn’t ignored:
2

Create the initial migration (only if the directory is empty)

3

Commit the whole directory

Commit every migration folder with its migration.sql and prisma/migrations/migration_lock.toml.

Every schema change

Commit the new migration together with the schema change.
  • If the change could drop data or fail on existing rows (for example a new unique field with duplicates), Prisma prints Warnings for the current datasource: and asks for confirmation. If the effect isn’t intended, answer n.
  • Changes that can’t be executed are reported as We found changes that cannot be executed: and nothing is created.
  • Never edit a migration that has been applied anywhere. Fix it with a new migration. Prisma treats a changed, already-applied migration as history drift.

After gato add, gato remove and gato update

Adding or removing a module changes prisma/schema/ (a module schema file and the wa:relations regions). The CLI prints the migration command to run next:
gato update suggests a migration only when the kit’s schema files changed between versions. Then run npm run db:generate and commit the migration with the rest of the change.
Removing a module drops its tables in the generated migration. Review the SQL before applying it anywhere with real data.
See gato add, gato remove and gato update.

Tests don’t use migrations

Integration tests build their schema with prisma db push; end-to-end tests use prisma db push --force-reset, or prisma migrate reset --force when migrations exist. Both wipe their database, so they refuse to run unless the database name ends in _test or _e2e respectively. See Testing.

Migration history drift

Prisma records applied migrations in the _prisma_migrations table. npx prisma migrate dev applies new migrations from prisma/migrations/, but refuses in two cases:
  • History mismatch: the database has an applied migration that’s missing from the directory, or whose file changed since it was applied.
  • Drift (Drift detected): the database schema differs from what the migrations produce, for example because tables were created by prisma db push.
Prisma then prints the reason, suggests prisma migrate reset, and exits without changing anything:

First, confirm it’s your database

On a fresh clone, this message often means you’re connected to someone else’s database, for example a local Postgres or another project’s container on the same port. Migration names you never created in the missing from the local migrations directory line are the strongest signal. A reset deletes everything in the database at DATABASE_URL, so confirm all of the following first:
  1. Address. The Datasource "db": … line shows localhost or 127.0.0.1, the port you expect (DB_PORT, default 5432) and your project’s database name from docker-compose.yml. A DATABASE_URL in your shell overrides .env.local, which overrides .env; check with echo "$DATABASE_URL".
  2. Port. After docker compose stop, nothing else is listening on that port.
  3. Repeatable. After docker compose up -d and a healthy db service, npx prisma migrate dev reports the same problem.
  4. History in the container. Reading the history directly inside your container matches Prisma’s message:
If any check fails, don’t reset anything. Find out what you’re connected to first.

What to do

Restore migrations from git:
If the deletion is already committed, use the commit before it as --source (find it with git log --oneline -- prisma/migrations).
In a project, never create a new init migration to replace deleted ones. It won’t match the history in your databases, and the committed migrations may already be applied on staging, production and teammates’ machines.
Back up before a reset (save the file outside the repo):
Reset only after all four checks pass, and never on a shared, staging or production database:
It asks for confirmation, then drops all data and applies the migrations from prisma/migrations/. Check the Datasource line once more before answering y.

Command reference