简体   繁体   中英

Prisma having issues seeding database with a 1 to many relationship

I have a table User and Upload with a 1 to Many relationship from Upload to User called avatar. When I seed my database with test data it will not let me use the create function to do it in 1 function. Instead, I have to do everything separately. I have examples below of code that works as well as the seed function that does not.

Here is my seed function that does not work:

    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,
        },
      }
    });

Here is the error vsCode throws on the entire userAvatar part.

(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); }'

Here are my relationships in 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
}

Here is code that actually works.

    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",
      }
    });

When I use avatar instead of userAvatar I get this error that is highlighted on create.

(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.

I can't assign owner because the const holding it is not wrapped up until this command is done. I also tried adding a random user as owenr and get this error.

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

The error is pretty descriptive:

Object literal may only specify known properties, and 'userAvatar' does not exist in type '(Without<UserCreateInput, UserUncheckedCreateInput> & UserUncheckedCreateInput) | (Without<...> & UserCreateInput)'.ts(2322)

Your User model does not have a userAvatar field defined, only avatar . If you are going to create an Upload model as part of seeding the database, you have to use fields Prisma knows about. It doesn't know about a userAvatar field, but it does know about an avatar field, so I think you just have to change the name of the field you are trying to create:

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,
        },
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM