postgresql Prisma在使用一对多关系设定数据库种子时出现问题

zkure5ic  于 2022-12-22  发布在  PostgreSQL
关注(0)|答案(1)|浏览(237)

我有一个名为avatar的表User和Upload,从Upload到User之间存在一对多关系。当我用测试数据作为数据库的种子时,它不允许我使用create函数在一个函数中完成它。相反,我必须单独完成所有事情。下面是一些代码示例,它们与seed函数一样有效。
下面是我的seed函数不起作用:

const janiceUser = await prisma.user.create({
      data: {
        email: 'janice123@yahoot.com',
        password: 'fake-password-that-is-not-hashed!',
        username: 'Janice.Kling78',
        name: 'Janice Bogan PhD',
        bio: 'I am a test user!',
        userAvatar: {
          create: {
            key: 'images/janiceAvatar.jpg',
            type: "IMAGE",
          },
        },
        include: {
          userAvatar: true,
        },
      }
    });

下面是vsCode在整个userAvatar部分上抛出的错误。

(property) userAvatar: {
    create: {
        key: string;
        type: string;
    };
}
Type '{ email: string; password: string; username: string; name: string; bio: string; userAvatar: { create: { key: string; type: string; }; }; userBanner: { create: { key: string; type: string; }; }; preferences: { ...; }; include: { ...; }; }' is not assignable to type '(Without<UserCreateInput, UserUncheckedCreateInput> & UserUncheckedCreateInput) | (Without<...> & UserCreateInput)'.
  Object literal may only specify known properties, and 'userAvatar' does not exist in type '(Without<UserCreateInput, UserUncheckedCreateInput> & UserUncheckedCreateInput) | (Without<...> & UserCreateInput)'.ts(2322)
index.d.ts(3567, 5): The expected type comes from property 'data' which is declared here on type '{ select?: UserSelect | null | undefined; include?: UserInclude | null | undefined; data: (Without<UserCreateInput, UserUncheckedCreateInput> & UserUncheckedCreateInput) | (Without<...> & UserCreateInput); }'

下面是schema.prisma中的关系

model User {
  id                Int               @id @default(autoincrement())
  username          String            @unique
  avatar            Upload?           @relation("userAvatar", fields: [avatarId], references: [id], onDelete: Cascade)
  avatarId          Int?
}

model Upload {
  id               Int         @id @default(autoincrement())
  userId           Int
  owner            User        @relation("uploads", fields: [userId], references: [id], onDelete: Cascade)
  key              String
  userAvatars      User[]      @relation("userAvatar")
  userBanners      User[]      @relation("userBanner")
  type             MediaType
}

这是真正有效的代码。

const janiceUser = await prisma.user.create({
      data: {
        email: 'janice123@yahoot.com',
        password: 'fake-password-that-is-not-hashed!',
        username: 'Janice.Kling78',
        name: 'Janice Bogan PhD',
        bio: 'I am a test user!',
      }
    });
    const janiceAvatar = await prisma.upload.create({
      data: {
          userId: janiceUser.id,
          key: 'images/janiceAvatar.jpg',
          type: "IMAGE",
      }
    });

当我使用avatar而不是userAvatar时,我会在创建时突出显示此错误。

(property) create?: (Prisma.Without<Prisma.UploadCreateWithoutUserAvatarsInput, Prisma.UploadUncheckedCreateWithoutUserAvatarsInput> & Prisma.UploadUncheckedCreateWithoutUserAvatarsInput) | (Prisma.Without<...> & Prisma.UploadCreateWithoutUserAvatarsInput) | undefined
Type '{ key: string; type: "IMAGE"; }' is not assignable to type '(Without<UploadCreateWithoutUserAvatarsInput, UploadUncheckedCreateWithoutUserAvatarsInput> & UploadUncheckedCreateWithoutUserAvatarsInput) | (Without<...> & UploadCreateWithoutUserAvatarsInput) | undefined'.
  Type '{ key: string; type: "IMAGE"; }' is not assignable to type 'Without<UploadUncheckedCreateWithoutUserAvatarsInput, UploadCreateWithoutUserAvatarsInput> & UploadCreateWithoutUserAvatarsInput'.
    Property 'owner' is missing in type '{ key: string; type: "IMAGE"; }' but required in type 'UploadCreateWithoutUserAvatarsInput'.ts(2322)
index.d.ts(29168, 5): 'owner' is declared here.

我不能分配所有者,因为保存它的常量在这个命令完成之前没有打包。我还尝试添加一个随机用户作为owenr,但得到了这个错误。

(property) owner: Prisma.UserCreateNestedOneWithoutUploadsInput
Type 'User' has no properties in common with type 'UserCreateNestedOneWithoutUploadsInput'.ts(2559)
but5z9lq

but5z9lq1#

这个错误非常具有描述性:
对象文本只能指定已知属性,并且类型“”中不存在“”userAvatar“”(没有〈UserCreateInput,UserUncheckedCreateInput〉& UserUncheckedCreateInput)“”|(没有<...>用户创建输入(& U)). ts(2322)
您的User模型没有定义userAvatar字段,只有avatar。如果您要创建Upload模型作为数据库播种的一部分,则必须使用Prisma知道的字段。它不知道userAvatar字段,但它知道avatar字段。所以我认为你只需要改变你试图创建的字段的名称:

const janiceUser = await prisma.user.create({
    data: {
        email: 'janice123@yahoot.com',
        password: 'fake-password-that-is-not-hashed!',
        username: 'Janice.Kling78',
        name: 'Janice Bogan PhD',
        bio: 'I am a test user!',
        // ------ changed userAvatar to userAvatars -----
        userAvatars: {
            create: {
                key: 'images/janiceAvatar.jpg',
                type: "IMAGE",
            },
        },
        // ------ changed userAvatar to userAvatars -----
        include: {
            userAvatars: true,
        },
    }
});

相关问题