contract infer
contract infer inspects a live database and writes a starter PSL contract. An existing file at the output path is overwritten, with a warning.
Use it when you are adding Prisma ORM to an existing database and want an initial contract to review and edit.
bunx prisma contract infer [--db "$DATABASE_URL"]pnpm prisma contract infer [--db "$DATABASE_URL"]yarn prisma contract infer [--db "$DATABASE_URL"]npx prisma contract infer [--db "$DATABASE_URL"]| Option | What it does |
|---|---|
--db <url> |
Connects to the database. Optional: without it the command uses db.connection from prisma.config.ts, and fails only when neither is set. |
--output <path> |
Writes the inferred PSL contract to a specific path. |
--config <path> |
Read this config file instead of ./prisma.config.ts. |
--json |
Prints a machine-readable result. |
bunx prisma contract infer --db "$DATABASE_URL"
bunx prisma contract infer --db "$DATABASE_URL" --output ./prisma/contract.prisma
bunx prisma contract infer --db "$DATABASE_URL" --jsonpnpm prisma contract infer --db "$DATABASE_URL"
pnpm prisma contract infer --db "$DATABASE_URL" --output ./prisma/contract.prisma
pnpm prisma contract infer --db "$DATABASE_URL" --jsonyarn prisma contract infer --db "$DATABASE_URL"
yarn prisma contract infer --db "$DATABASE_URL" --output ./prisma/contract.prisma
yarn prisma contract infer --db "$DATABASE_URL" --jsonnpx prisma contract infer --db "$DATABASE_URL"
npx prisma contract infer --db "$DATABASE_URL" --output ./prisma/contract.prisma
npx prisma contract infer --db "$DATABASE_URL" --jsonInference gives you a starting point, not a finished design. Review:
- model and field names
- relation names
- mapped database names
- defaults, indexes, and constraints
- extension-backed column types
The file starts with // use prisma-8, because contract emit reads only .prisma files that start with that line. Each model is named after its table, and gets @@map only when the table name differs from the model name: a table User becomes model User, and a table user_profile becomes model UserProfile with @@map("user_profile").
Every column default is written in a form contract emit accepts, so you can emit the file without editing it. Default values shows those forms. A list column that allows NULL is written Type[]?, and a list column that is NOT NULL is written Type[].
If you keep an inferred contract in version control, running contract infer again after an upgrade can write the same database differently, so review the diff before you commit it.
The command stops at contract.prisma. Follow it with the emit and sign steps:
bunx prisma contract emit
bunx prisma db sign --db "$DATABASE_URL"
bunx prisma db verify --db "$DATABASE_URL"pnpm prisma contract emit
pnpm prisma db sign --db "$DATABASE_URL"
pnpm prisma db verify --db "$DATABASE_URL"yarn prisma contract emit
yarn prisma db sign --db "$DATABASE_URL"
yarn prisma db verify --db "$DATABASE_URL"npx prisma contract emit
npx prisma db sign --db "$DATABASE_URL"
npx prisma db verify --db "$DATABASE_URL"db sign is the handoff point where you record that the existing database matches the reviewed contract.