node.js | 使用Prisma替换DB客户端并保留DB结构 - uuid_generate_v4()问题

3

我正在尝试将Prisma DB客户端添加到现有的Node.js项目中,同时保留数据库结构。

Postgresql Prisma 4.7.1

  1. 我已经设置了初始的Prisma配置(环境变量等)。
  2. 我使用命令npx prisma db pull生成了根据现有数据库结构创建的prisma.schema文件。
  3. 我通过使用一些空的数据库创建了初始迁移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") }

后续计划如下:

  1. 将 DB 设置回包含表和数据的原始状态
  2. 然后使用以下命令将初始迁移标记为已应用 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
}

有什么办法解决这个问题吗?提前致谢!

1个回答

2

我在发布了上述问题后不久就找到了解决方案,虽然有些困难。

我找到了一些提示,但最初它们对我来说有点不清楚。

所以如果有人遇到类似的问题,那么有一个解决方案:

  1. 忽略第3步中的错误 - 迁移文件仍会被创建
  2. 然后将 CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; 添加到创建的迁移文件的第一行。
  3. 再次运行 npx prisma migrate dev,这次应该没有错误
  4. 继续执行上面提到的第4步。

希望这可以帮助到你 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接