Troubleshooting Prisma DB Seeding Failure
This note documents the process of resolving a PrismaClientKnownRequestError that occurred during database seeding. The root cause was a mismatch between the Prisma schema and the actual database state.
1. The Initial Problem: Seeding Fails
When running pnpm prisma db seed, the process failed with an error indicating a missing column in the database.
pnpm prisma db seed
Error Output:
Error during seeding: PrismaClientKnownRequestError:
Invalid `prisma.cart.create()` invocation in
/path/to/project/packages/database/prisma/seed.ts:804:35
→ 804 const cart1 = await prisma.cart.create(
The column `promo_discount_amount` does not exist in the current database.
code: 'P2022',
meta: { modelName: 'Cart', column: 'promo_discount_amount' },
clientVersion: '6.16.2'
}
This error (P2022) clearly shows that the seed.ts script was trying to insert data into the promo_discount_amount column of the Cart table, but this column did not exist in the database.
2. Failed Attempts to Fix
Several attempts were made to fix the issue using standard Prisma migration commands, but they failed due to the database being in a broken or inconsistent state.
Attempt A: prisma migrate dev
The first instinct was to create a new migration to add the missing column.
pnpm prisma migrate dev --name add_promo_discount_amount_to_cart
This failed because a previous, unsuccessful migration had left the database in a state that prevented new migrations from being applied.
Attempt B: prisma migrate reset
The next step was to reset the database, hoping to clear the bad state and re-apply all migrations from scratch.
pnpm prisma migrate reset
While the reset completed, the subsequent automatic seeding failed with the exact same error. This happened because migrate reset only applies existing migration files. Since a migration for promo_discount_amount was never successfully created, the column was still missing after the reset.
Attempt C: Resolving and Deleting Failed Migrations
Further attempts to resolve the failed migration state and manually delete the broken migration files also led to a dead end, with Prisma detecting a discrepancy and requiring another reset.
3. The Resolution: Clean Up the Migration State, Then Re-migrate
The key insight was that Prisma's migration history lives on disk (the
prisma/migrations folder) while the actual schema lives in the database. When those two
disagree — a failed migration file exists, or a schema change was never captured as a migration —
Prisma refuses to proceed.
1. Remove the broken migration state so Prisma starts from a clean history:
rm -rf prisma/migrations # or remove only the broken migration folder
If you want to keep the history of good migrations and remove only the bad ones, see how to manually delete the migration folder.
2. Recreate a migration from the current schema — this captures the Cart table with the
promo_discount_amount column as it exists in schema.prisma:
pnpm prisma migrate dev --name init_from_clean_schema
3. Confirm the column now exists, then run the seed again:
pnpm prisma db seed
This time the seed completed: the database contained every column the seed.ts script was
writing to, so prisma.cart.create() succeeded.
4. Root Cause Summary
The P2022 error meant "the column does not exist in the database", and the reason it
survived a migrate reset was that migrate reset re-applies existing
migration files only. Because no migration for promo_discount_amount was ever
successfully created, resetting could not add the column. The schema change had to be captured as a
migration first.
5. How to Prevent This
- Always run
prisma migrate dev(and commit the generated migration folder) whenever the schema changes — the migration files are the contract between schema and database. - Never edit the database directly while using migrations; Prisma sees a drift it cannot resolve.
- Check the migration history (
prisma migrate status) before running a seed — a red "migrations not applied" state predicts exactly this failure. - In CI, run
prisma migrate deploy(which applies committed migrations) before seeding instead ofmigrate dev.
Conclusion
The seed failed because the database was missing a column the schema declared — and no migration file existed to create it. Attempts to fix it by migrating or resetting could not work, because both only apply what is already in the migration history. Clearing the broken migration state and generating a fresh migration from the current schema aligned all three layers (schema, migrations, database), and the seed ran cleanly.