我正在尝试将Prisma DB客户端添加到现有的Node.js项目中,同时保留数据库结构。
Postgresql Prisma 4.7.1
- 我已经设置了初始的Prisma配置(环境变量等)。
- 我使用命令
npx prisma db pull
生成了根据现有数据库结构创建的prisma.schema
文件。 - 我通过使用一些空的数据库创建了初始迁移
npx prisma migrate dev
在这一点上,预期迁移应该创建数据库结构,但是该命令失败并显示以下错误信息:
✗ 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") }
后续计划如下:
- 将 DB 设置回包含表和数据的原始状态
- 然后使用以下命令将初始迁移标记为已应用
npx prisma migrate resolve --applied 20221221095823_init
因此,主要问题在于现有表中的 ID 使用 uuid_generate_v4() 生成新条目的随机 UUID。数据库级别的支持绝对存在,因为它可以与 slonik 数据库客户端正常工作。
model SomeTable {
id String @id @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
}
有什么办法解决这个问题吗?提前致谢!