# Add a Seed Script to a Prisma Project

Yes. An agent writes a prisma/seed.ts that fills your database with the users and rows your app's own rules expect, wires it to prisma db seed, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-seed-data/prisma/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent starts by reading the schema and every place the app reads from it, because a seed that ignores the app's own rules produces a database nobody can use. In a Next.js app laid out like shadcn-ui/taxonomy, `prisma/schema.prisma` has a MySQL datasource and five models: `Account`, `Session`, `User`, `VerificationToken` and `Post`, with `posts.authorId` a foreign key to `users` that is `ON DELETE RESTRICT`, so posts must be written after their author and deleted before them. `package.json` pins `prisma` and `@prisma/client` at 4.13, runs `prisma generate` on `postinstall`, and has no `prisma.seed` entry; `prisma/migrations/` holds two migrations, `20221021182747_init` and `20221118173244_add_stripe_columns`, so `prisma migrate reset` already works and would run a seed if one were declared. The lockfile is `pnpm-lock.yaml` and `.nvmrc` says Node 16.18.

Then it reads what a believable row looks like. `app/api/posts/route.ts` refuses a fourth post from a user whose plan is free, by counting `db.post` rows with the caller's `authorId`, and `lib/subscription.ts` decides a user is on the Pro plan when `stripePriceId` is set and `stripeCurrentPeriodEnd` is less than a day in the past. `components/editor.tsx` saves `content` as whatever EditorJS returns from `save()`, an object with `time`, `blocks` and `version`, so a seeded post needs a `blocks` array with `header` and `paragraph` entries, not a Markdown string. `app/(dashboard)/dashboard/page.tsx` orders posts by `updatedAt` descending, and `lib/auth.ts` signs people in through GitHub or an email link from Postmark, so a seeded user is reached by its `email`, which the schema marks `@unique`.

With that, the agent writes `prisma/seed.ts`. It imports only `@prisma/client`, not `@/env.mjs`, because the `createEnv` call there throws when Stripe and Postmark variables are missing and a seed should need `DATABASE_URL` alone. It upserts two users by email: one on the free plan with exactly three posts, so the limit in the posts route is visible the moment you sign in as them, and one with `stripePriceId`, `stripeSubscriptionId`, `stripeCustomerId` and a `stripeCurrentPeriodEnd` a month out, with a handful of posts, some `published` and some drafts. Each post gets a fixed `id` so a second run updates instead of duplicating, `createdAt` and `updatedAt` spread over several days so the dashboard order means something, and `content` in the EditorJS shape with a header block and two paragraph blocks. It adds `tsx` as a dev dependency, current release 4.23.15, and declares `"prisma": {"seed": "tsx prisma/seed.ts"}` in `package.json`, which is how Prisma 4 finds the script for `prisma db seed` and after `prisma migrate reset`.

To check it, the agent starts a MySQL server in its sandbox, points `DATABASE_URL` at it, runs `pnpm prisma migrate deploy` and then `pnpm prisma db seed` twice, and counts rows after each run to show the second run changed nothing. It runs `pnpm tsc --noEmit` so the seed compiles against the generated client, and `pnpm lint`. It does not run `prisma db push`, because the repository already has migrations and the seed must work on the schema they produce.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add a seed script to this Prisma project. Read prisma/schema.prisma, prisma/migrations/, package.json, lib/db.ts, lib/subscription.ts, config/subscriptions.ts, app/api/posts/route.ts, components/editor.tsx and lib/auth.ts first. Write prisma/seed.ts that imports only @prisma/client, upserts users by email so it is safe to run twice, seeds one free-plan user with exactly as many posts as the app's free limit allows and one Pro user with Stripe fields set and several published and draft posts, stores post content in the exact shape the editor saves, and gives every row a fixed id and spread-out timestamps. Add tsx as a dev dependency and a prisma.seed entry in package.json. Start a MySQL server in your sandbox, run prisma migrate deploy and prisma db seed twice, count rows after each run, run tsc --noEmit and lint, and open a pull request with the output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

The pull request adds `prisma/seed.ts` and changes `package.json` (the `prisma.seed` entry, `tsx` under `devDependencies`) and `pnpm-lock.yaml`. Its description lists the seeded users with their emails and plans, the number of posts each one has and why, the two `prisma db seed` runs with the row counts after each, and the `tsc` and lint output.

The seed is idempotent, so running it against a database that already has it changes nothing, and the free-plan user makes the post limit in `app/api/posts/route.ts` reproducible on a fresh database without touching Stripe. The schema and the migrations are unchanged; the run's record keeps the MySQL setup and every command.

Before merging, a reviewer checks that the emails used are addresses the team controls or clearly fake, decides whether the seed should also run in CI before the end-to-end tests, and makes sure nothing deploys with `prisma migrate reset` on a path that reaches production, since reset drops the database and then seeds it.

## Run it on every new Prisma project

A team that starts projects from a template sends this body once per repository to `POST /o/{org}/sessions`, each with its own repository in `resources` and a `request_id` that is a UUID v5 of the repository's full name, so a retried call returns the run already started instead of opening a second pull request. Each run reads its own schema: a project on PostgreSQL with `prisma migrate dev` gets the same script against the models it has, and one with an existing `prisma/seed.ts` gets the missing rows added to it rather than a replacement.

A `webhook_url` on the key reports each `run.completed` with the pull request in `result`, and a `run.failed` names a project whose migrations did not apply cleanly to an empty database, which is worth fixing before anyone seeds it.

## Why run it on Rush?

A seed script is quick to write and slow to get right: the rows have to satisfy the foreign keys, the app's own plan limits, the exact JSON shape a rich-text editor expects, and the unique constraints that make a second run safe. The agent reads the route handlers and the editor to learn those rules, and it proves the script on a real MySQL server in its own sandbox instead of on someone's laptop database.

Both seed runs, the row counts and the type check stay with the run and are pasted into the pull request, so the review is a diff plus evidence, not a request to try it locally. Several projects can get a seed at once, each from its own schema.

## Frequently asked questions

### Why tsx rather than ts-node?

The app is ESM-leaning with `next.config.mjs` and `env.mjs`, and `tsx` runs a TypeScript file without a separate compiler config. Prisma's own documentation shows both; the agent picks `tsx` and pins it, and the `prisma.seed` command is one line to change if the team prefers `ts-node`.

### Will prisma migrate reset run it automatically?

Yes. Once `prisma.seed` is declared in `package.json`, Prisma runs it after `migrate reset` and after `migrate dev` creates a fresh database, unless `--skip-seed` is passed. That is why the script has to be safe on an empty database and on one that already holds the rows.

### Does the seed create sessions so I can skip signing in?

No. NextAuth sessions carry a token that the browser must hold, so seeding the `sessions` table would not sign anyone in. The seed creates users reachable by email; signing in through the email provider or GitHub attaches the session to the seeded user because `lib/auth.ts` looks users up by email.

### What about the Stripe webhook data?

The Pro user gets the four Stripe columns the app reads, with plausible placeholder ids. Nothing is created in Stripe, and the ids will not resolve there, which is fine for the dashboard and the post limit but not for the billing portal link; the pull request says so.

## More Prisma jobs

- [Add Slow Query Logging to a Prisma App](https://getrush.ai/run/add-slow-query-logging/prisma/)
- [Add Soft Delete to a Prisma Schema](https://getrush.ai/run/add-soft-delete/prisma/)
- [Run Prisma Tests Against Their Own Database, Not the Dev One](https://getrush.ai/run/add-test-database-isolation/prisma/)

## The same job on other stacks

- [Add Seed Data to a FastAPI SQLModel App](https://getrush.ai/run/add-seed-data/sqlmodel/)
- [Write a Seed File for a Supabase Project](https://getrush.ai/run/add-seed-data/supabase/)
- [Add Seed Data to a NestJS TypeORM App](https://getrush.ai/run/add-seed-data/typeorm/)

## Keep reading

- [What an agent can do in Prisma itself, once you connect it](https://getrush.ai/integrations/prisma/)
- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
