【问题标题】:Automatically add UUID relation on prisma create在 prisma create 上自动添加 UUID 关系
【发布时间】:2021-09-03 18:43:37
【问题描述】:

我的 Postgres 数据库中有一个 UserUserProfilePost 模型。 UserUserProfile 相互依赖。我正在尝试创建一个User,它会自动创建一个UserProfile,但我似乎无法找到如何自动为UserProfile 关系假设User 的ID,我正在使用UUID对于User 模型。

架构模型

model User {
  id             String       @id @default(uuid())
  createdAt      DateTime     @default(now())
  username       String       @unique @db.VarChar(20)
  emailEncrypted String       @unique
  emailIv        String       @unique
  password       String
  isMod          Boolean      @default(false)
  isAdmin        Boolean      @default(false)
  emailConfirmed Boolean      @default(false)
  profile        UserProfile?
}

model UserProfile {
  user     User      @relation(fields: [id], references: [id])
  id       String    @unique
  posts    Post[]
  comments Comment[]
}

model Post {
  id        String      @id @default(uuid())
  createdAt DateTime    @default(now())
  title     String      @db.VarChar(300)
  caption   String      @db.VarChar(1000)
  upvotes   Int         @default(0)
  downvotes Int         @default(0)
  comments  Comment[]
  author    UserProfile @relation(fields: [authorId], references: [id])
  authorId  String
}

查询

const user = await prisma.user.create({
    data: {
        username,
        emailEncrypted: encrypted,
        emailIv: iv,
        password: hashedPassword,
        profile: {
            create: {}, // create a UserProfile that assumes the created user's UUID as a relation
        },
    },
    select: {
        id: true,
    },
});

如您所见,我尝试使用create: {} 来假设用户的UUID,但它无法创建实际的UserProfile,只是User,这当然会破坏系统。

【问题讨论】:

    标签: node.js postgresql prisma


    【解决方案1】:

    我也运行过这个。为了在创建用户后包含profile 数据,我做了以下调整。

    const user = await prisma.user.create({
        data: {
            username: "username",
            emailEncrypted: "encrypted",
            emailIv: "iv",
            password: "hashedPassword",
            profile: {
                create: {},
            },
        },
        select: {
            id: true,
            createdAt: true,
            emailConfirmed: true,
            emailIv: true,
            emailEncrypted: true,
            isAdmin: true,
            isMod: true,
            password: true,
            profile: true,
            username: true,
        },
    });
    

    这个输出,

    没有问题,它工作正常。

    仍然想知道您面临的问题是什么。

    你能粘贴错误日志吗?

    【讨论】:

      【解决方案2】:

      这是在 Prisma 中添加关系的正确方法。您使用create 添加关系,UUID 将自动添加到该记录中。您无需执行任何其他操作。我尝试了以下方法,对我来说效果很好:

      【讨论】:

      • 谢谢,Prisma 不会自动创建 UserProfile,因为 UserProfile 是可选的,我该如何解决这个问题?
      • 如果您在对象中传递profile: { create: { // required fields },那么 Prisma 将自动创建 UserProfile。
      • 谢谢,已经尝试过了,但是在尝试获取UserProfiles 时出现PANIC 错误。
      • 错误是什么?你的@prisma/clientprisma 也是最新版本吗?
      • 我正在使用最新版本的 Prisma。我现在在与用户一起创建帖子时遇到空约束违规错误。我可以为用户单独创建一个用户配置文件,但这不是最好的方法。
      猜你喜欢
      • 2023-02-06
      • 2019-11-22
      • 1970-01-01
      • 2022-11-15
      • 2022-08-03
      • 2022-01-06
      • 2021-10-28
      • 2021-03-17
      • 2022-11-25
      相关资源
      最近更新 更多