How to use Prisma migrations to add new models on an existing Laravel-managed MySQL database without data loss #28943
Replies: 6 comments 5 replies
|
Hey @Pavan-Kumar-KN! After you have applied have marked the baseline migration as applied via this command: What happens if you run |
|
Hi @nurul3101, The issue I’m facing is that when I run prisma migrate dev, Prisma suggests resetting the database or running migrate reset, and also throws relation / foreign key errors—even though the current schema and database state are already in sync. Since this is a shared database with existing data, resetting the DB is not an option. I’m trying to understand the correct and safe workflow to introduce Prisma migrations without affecting the existing Laravel-managed schema. although i can push the new models using push command or deploy command but the migration file we have to maintain right |
|
Hey @Pavan-Kumar-KN, thanks for the additional context! I understand the issue now. When you run Here's what I'd recommend:
Regarding migration files: Yes, you should maintain them! Using Let me know if you still see the reset warnings after ensuring your baseline matches the DB state exactly! |
|
hello @nurul3101 , I found that this case was caused by the
as the current db connection user have the create/drop db auth , it will help create the shadow db with the randon db name, but when I going to point an specified db as the shadow db by the
so to resolve the issue caused by when I config |
|
For a shared existing DB, the key point is: The safe long-term flow is usually:
If you still get reset/drift warnings after baselining, it usually means either the baseline does not exactly match the real DB, or the shadow DB setup/permissions are off. |
|
This is a well-documented but easy to misconfigure scenario. The warning you are seeing is the key diagnostic, so understanding exactly why it appears will clarify which step needs fixing. Why Prisma is still warning about data loss When you run The most common cause in your situation is that the baseline migration SQL in Fixing the baseline Before adding any new models, verify the baseline is accurate by running: npx prisma migrate diff \
--from-migrations prisma/migrations \
--to-schema-datasource prisma/schema.prisma \
--shadow-database-url "postgresql://user:pass@localhost/shadow_db"If this outputs any SQL, your baseline does not match reality. The safest way to regenerate an accurate baseline is: npx prisma db pullThis overwrites your npx prisma migrate diff \
--from-empty \
--to-schema-datasource prisma/schema.prisma \
--script > prisma/migrations/0_init/migration.sqlThen mark it applied again: npx prisma migrate resolve --applied 0_initAdding new models safely after a clean baseline Once the baseline is accurate and drift-free, your workflow for new Prisma-only tables is: npx prisma migrate dev --create-only --name add_new_tableReview the generated SQL to confirm it only contains npx prisma migrate deployUse Long-term workflow for Laravel and Prisma sharing the same database Keep a strict ownership boundary. Laravel owns its tables completely and Prisma should never reference them in // Only define models Prisma is responsible for
// Do not add models for Laravel tables
model PrismaOwnedTable {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
}If you need to query Laravel tables from Prisma, use Your migration commands by environment should be: # Local development - creates migration file only, you review before applying
npx prisma migrate dev --create-only --name descriptive_name
# Staging and production - applies pending migrations only, no destructive logic
npx prisma migrate deployNever run |
|
This is a classic Prisma baselining + shadow database drift scenario. Here's the correct step-by-step approach to add new Prisma models to a Laravel-managed MySQL database without any data loss or resets: The correct workflow:Step 1: Make sure your baseline is solid Your baseline migration ( # Pull the exact current schema from your live DB into schema.prisma
npx prisma db pull
# Then generate your baseline SQL from this
npx prisma migrate diff \
--from-empty \
--to-schema-datamodel prisma/schema.prisma \
--script > prisma/migrations/0_init/migration.sql
# Mark it as already applied
npx prisma migrate resolve --applied 0_initStep 2: Add your new models to Only add the new tables — do NOT modify or remove existing Laravel-managed table definitions. Step 3: Use npx prisma migrate dev --create-only --name add_new_modelsThis creates Step 4: Apply the migration directly (safe) npx prisma migrate deploy
Why
|


Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
What I have done so far
The database already exists and contains data.
I created a baseline migration in Prisma that represents the current schema:
I marked the baseline migration as applied.
I updated
schema.prismaand added new models (new tables only, no changes to existing Laravel tables).When I run:
Prisma warns that:
The database will be reset, or
Data loss may occur
This is not acceptable because the database is shared and already in use.
What I want
Prisma should treat the existing schema as baseline
Prisma migrations should be used only for new tables / new models
No destructive changes
No database reset
A clean, safe workflow going forward
My Questions:
What is the correct Prisma workflow for adding new models to an already existing Laravel-managed database?
How should the baseline migration be created and applied so Prisma does not attempt a reset?
After baselining, should prisma migrate dev be used for new models, or is migrate deploy the correct command?
What is the recommended long-term migration strategy when Laravel and Prisma share the same database but Prisma is responsible for new tables?
Any official guidance or best practices for this scenario would be very helpful.
All reactions