I am trying to add the Prisma DB client to the existing node.js project while preserving the DB structure.
Postgresql Prisma 4.7.1
npx prisma db pull
to generate prisma.schema
file according to the existing DB structurenpx prisma migrate dev
At this point it is expected that migration would create DB structure, but the command fails with the following error
✗ npx prisma migrate dev
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "service_prisma", schema "public" at "127.0.0.1:5432"
PostgreSQL database service_prisma created at 127.0.0.1:5432
✔ Enter a name for the new migration: … init
Applying migration `20221221095823_init`
Error: P3018
A migration failed to apply. New migrations cannot be applied before the error is recovered from. Read more about how to resolve migration issues in a production database: https://pris.ly/d/migrate-resolve
Migration name: 20221221095823_init
Database error code: 42883
Database error:
ERROR: function uuid_generate_v4() does not exist
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
DbError { severity: "ERROR", parsed_severity: Some(Error), code: SqlState(E42883), message: "function uuid_generate_v4() does not exist", detail: None, hint: Some("No function matches the given name and argument types. You might need to add explicit type casts."), position: None, where_: None, schema: None, table: None, column: None, datatype: None, constraint: None, file: Some("parse_func.c"), line: Some(521), routine: Some("ParseFuncOrColumn") }
Follow up plan would be to:
npx prisma migrate resolve --applied 20221221095823_init
So, main problem is that IDs in existing tables use uuid_generate_v4() to generate random UUID for new entries. The support on DB level is definitly there, because it simple works normally with slonik DB client.
model SomeTable {
id String @id @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
}
Any idea how to solve this? Thanks in advance!
I struggled a bit, but then found a solution shortly after posting the question above...
I've found some tips but they were a bit unclear to me initially.
So if anyone runs into a similar problem, then there is a solution:
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
into the first line of the created migration file.npx prisma migrate dev
again, this time it should run without errorsHope this helps :)