【问题标题】:Prisma 1-1 relationship can't access data from both sidesPrisma 1-1 关系无法从双方访问数据
【发布时间】:2022-02-17 08:16:20
【问题描述】:

所以我在Nest 项目中有以下棱镜架构设置:

model users {
    id                  Int                 @id @default(autoincrement())
    uuid                String              @default(uuid())
    createdAt           DateTime            @default(now())
    updatedAt           DateTime            @updatedAt
    firstName           String
    authentication      authentications    @relation("Authentication", fields: [authenticationId], references: [id], onDelete: Cascade)
    authenticationId    Int
}

model authentications {
    id                          Int         @id @default(autoincrement())
    uuid                        String      @default(uuid())
    createdAt                   DateTime    @default(now())
    updatedAt                   DateTime    @updatedAt
    role                        Role        @default(USER)
    emailAddress                String      @unique
    password                    String
    isEmailConfirmed            Boolean     @default(false)
    currentHashedRefreshToken   String?
    user                        users?      @relation("Authentication")
}

enum Role {
  SUSPENSION    @map("SUSPENSION_ROLE")
  USER          @map("USER_ROLE")
  ADMIN         @map("ADMIN_ROLE")
}

现在我想从数据库中获取users。所以我打电话给

await this._prismaService.users.findMany({});

...它返回未定义。

但是当我使用authentications 执行相同的语句时,它可以工作。 像这样:

await this._prismaService.authentications.findMany({});

有人可以向我解释为什么会这样吗? 我只想检查用户是否具有给定的 UUID。 感谢您的帮助!

【问题讨论】:

  • 当您在“pgAdmin4”之类的工具中使用 SQL 语句查询 PostgreSQL 表时,您是否得到了预期的结果?
  • @StefanWuebbe 是的,当我直接查询数据库时,我得到了预期的结果。我使用了 Jetbrains 的 IDE DataGrip 和这条 SQL 语句:SELECT * FROM authentication.users 由于 1-1 关系,查询是否可能仅在身份验证的一侧起作用?据我了解,它应该从双方都起作用。
  • 很难说... 作为一名 C# 程序员,我可能会问,当您通过主键唯一行标识符查找单个用户时,为什么要使用 findMany({})... 但是我不是netjs 专家,所以不要听角落里的人的话。
  • 尝试 findFirst() 或 findUnique() 都没关系。它总是为用户返回 undefined :(
  • 很难相信findMany() 返回未定义。它不可能这样做,因为它总是返回一个数组。您可能在某处有错字或查看了错误的日志语句。

标签: postgresql nestjs relationship one-to-one prisma


【解决方案1】:

我通过仔细检查我所有的 Prisma 设置解决了这个问题。在 Danila 发表评论后,我注意到我的中间件做错了导致问题的原因。

【讨论】:

    猜你喜欢
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 2011-06-12
    • 2022-12-22
    • 2020-09-02
    • 2020-05-08
    相关资源
    最近更新 更多