Drift detected after making fields required
My db is cockroach.
I added two models,
Initially,
file_link_id and file_link were nullable. My next migration made those fields required. Migration was a success. However, then I added some fields. This was my new schema:
And then I got this error:
reseting is not an option. How can I resolve this?4 Replies
You opted for speed from this bot. Let's conjure up an answer right now!
You are seeing drift because your migration history and your actual database schema are out of sync—specifically, the nullability of the file_link_id column in FontVariant has changed back and forth, and Prisma Migrate is detecting this as drift.
How to resolve without resetting the database:
1. Introspect the current database schema:
This updates your Prisma schema to match the current state of the database.
2. Generate a baseline migration:
Use
prisma migrate diff to create a migration that matches your current schema:
3. Mark the migration as applied:
4. Now, make your desired schema changes (add new fields, change nullability, etc.) in your Prisma schema file.
5. Create and apply a new migration:
This process "baselines" your current database, telling Prisma that the existing schema is the starting point, and future migrations will be tracked from there—no reset required. This is the recommended approach for existing databases where data loss is not acceptable see official guidance, discussion, and docs.
Note: If you encounter issues with migration names, ensure you use the exact folder name created in the migrations directory when running migrate resolve.
If you have further manual changes or drift, repeat the introspect and baseline process as needed.
🤔 Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly.
In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.To confirm, the bot response of Baselining wasn't helpful?
That is correct, but I did find the fix:
After trying a dozen different things, creating a migration manually seems to do the trick:
1) Create the migration
mkdir -p prisma/migrations/20250728130000_enforce_file_link_required
cat > prisma/migrations/20250728130000_enforce_file_link_required/migration.sql <<EOF
ALTER TABLE "FontVariant" ALTER COLUMN "file_link_id" SET NOT NULL;
EOF
2) Tell Prisma it’s already been applied
npx prisma migrate resolve \
--applied 20250728130000_enforce_file_link_required
3) Verify / run
npx prisma migrate status
npx prisma migrate dev
Thanks for sharing your fix, it will be helpful to someone who might run into this