P
Prisma5mo ago
Samtuga1

How to run safe migrations

hello guys I am new to Backend. i am using prisma. and I have a hairstyle table containing hairstyles and now I added a business to the hairstyle table that references the business table. now I want to run migration but it’s giving me an error that the previous hairstyles in my database references null businesses. how do i solve this
2 Replies
Olyno
Olyno5mo ago
Hi :vmathi: First create a new migration:
npx prisma migrate dev --create-only
npx prisma migrate dev --create-only
Then add your migration to the new created file. If you want to set a default value for the new field for existing records, you can add an UPDATE SQL command before the ALTER TABLE command that adds the new field. For example, if you're adding a businessId field to the hairstyle table and want to set a default value of NULL for existing records, you could add:
UPDATE hairstyle SET businessId = NULL;
UPDATE hairstyle SET businessId = NULL;
If you want to remove records that do not have corresponding references, you can add a DELETE SQL command. For example:
DELETE FROM hairstyle WHERE businessId IS NULL;
DELETE FROM hairstyle WHERE businessId IS NULL;
Whenever you're ready, run your new migration:
npx prisma migrate dev
npx prisma migrate dev
Samtuga1
Samtuga15mo ago
This makes much sense. Thank you very much 👍
Want results from more Discord servers?
Add your server