psql - Prisma 与 psql db - 不兼容的类型 : text and uuid

标签 psql prisma prisma-graphql

我正在尝试学习如何将 prisma 与 psql 数据库一起使用。

我在使用 id 是 uuid 字符串的引用时遇到问题。

我有一个用户模型:

model User {
  id        String   @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  request   Request?
  createdAt DateTime @default(now()) @db.Timestamptz(6)
  updatedAt DateTime @default(now()) @updatedAt @db.Timestamptz(6)
}

model Request {
  id                Int @id @default(autoincrement())
  user              User   @relation(fields: [id], references: [id])
  // I also tried making the relation field userId
  createdAt         DateTime @default(now()) @db.Timestamptz(6)
  updatedAt         DateTime @default(now()) @updatedAt @db.Timestamptz(6)
}

当我尝试迁移它时,我收到一条错误消息:

failed to apply cleanly to the shadow database. Error: db error: ERROR: foreign key constraint "Request_userId_fkey" cannot be implemented DETAIL: Key columns "userId" and "id" are of incompatible types: text and uuid.

prisma 文档没有显示使用 uuid 的示例。

他们给出的例子在 Profile 模型中有第二个参数,它有一个作为 Int 的 userId。我尝试将其添加到我的 Request 模型中(作为 int、字符串和 uuid)。这些都不起作用。

model User {
  id      Int      @id @default(autoincrement())
  email   String   @unique
  name    String?
  role    Role     @default(USER)
  posts   Post[]
  profile Profile?
}

model Profile {
  id     Int    @id @default(autoincrement())
  bio    String
  user   User   @relation(fields: [userId], references: [id])
  userId Int
}

使用 uuid 生成的 userId 如何引用?

segment of the prisma documentation建议(如果我理解正确的话),任何 String 或 Int 或 enum 都应该能够识别 uuid:

Relational databases Corresponding database type: PRIMARY KEY

Can be annotated with a @default() value that uses functions to auto-generate an ID:

autoincrement() cuid() uuid() Can be defined on any scalar field (String, Int, enum)

当我尝试将 pgcrypto 扩展添加到 psql 时,我尝试再次运行迁移并收到一个错误消息,虽然消息不那么冗长,但仍然是类似的问题:

Error parsing attribute "@relation": The type of the field id in the model Request is not matching the type of the referenced field id in model User.

我看过this discussion这表明以某种方式对 prisma 撒谎。我不够聪明,无法理解谎言应该是什么或如何撒谎的要点。

github 上有人建议在请求模型中使用这种引用语法:

user                   User                      @relation(fields: [userId], references: [id])
userId                 String                    @unique    @db.Uuid

我按上面的方法试过了,没有 @unique 标志,但我仍然收到一个迁移错误,提示 uuid 和 text 是不兼容的引用。我找不到 prisma 文档的一部分来说明如何使 uuid 引用与关系模型兼容。

仅供引用:上述尝试的迁移文件显示如下:

CREATE TABLE "Request" (
    "id" SERIAL NOT NULL,
    "userId" UUID NOT NULL,
    
    CONSTRAINT "Request_pkey" PRIMARY KEY ("id")
);

最佳答案

您必须在引用列userId 上使用注解@db.Uuid,阅读more about it here .

例子:

model Request {
  id                Int @id @default(autoincrement())
  user              User   @relation(fields: [userId], references: [id])
  userId            String @db.Uuid
  ...your other stuff
}

关于psql - Prisma 与 psql db - 不兼容的类型 : text and uuid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71043504/

相关文章:

linux - postgresql 中的计划查询执行

Prisma 模式 - 从多个可能的外键(或关系)创建关系字段

node.js - Prisma:使用具有非唯一字段的 "where"进行更新?

graphql - 错误: Valid values for the strategy argument of `@scalarList` are: RELATION

javascript - Nexus-prisma:订购嵌套连接

python - docker-compose psql 无法连接到服务器

php - Laravel 中 pgsql 上的 Cacti

javascript - Prisma 中的数据建模与关系

graphql - 如何在 GraphQL Playground 中执行变更?

sql - 从命令 shell 处理 COPY 中的引号到 CSV