node.jsprismaprisma2

Make a change to the database with Prisma.js without having to reset the whole thing


How can make a change to the database with Prisma.js without having to reset the whole thing?

if I have used this command

npx prisma migrate dev --name role-makeOptional-NormalizedName

I will lose all of the data in my database but I don't lose my data.

In my case I wanted to change String to String? in schema.prisma file

NormalizedName  String?            @unique @db.VarChar(64)

Is there a proper command to avoid losing the data?


Solution

  • Go into your schema and make the change:

    NormalizedName  String            @unique @db.VarChar(64)
    NormalizedName  String?            @unique @db.VarChar(64)
    

    Then create a draft migration:

    $ npx prisma migrate dev --name migration-name --create-only
    

    Then edit the migration in SQL (this allow null values ie optional)

    ALTER TABLE myTable ALTER COLUMN myColumn {DataType} NULL;
    

    OR Postgres

    ALTER TABLE myTable ALTER COLUMN myColumn DROP NOT NULL;
    

    etc.

    Then apply the modified SQL:

    $ npx prisma migrate dev
    

    I hope this works for you :)