[英]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.我有一个表 User 和 Upload 从 Upload 到 User 称为 avatar 的一对多关系。 When I seed my database with test data it will not let me use the create function to do it in 1 function.
当我用测试数据为我的数据库播种时,它不会让我使用 create 函数在 1 个函数中完成它。 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.这是 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); }'
Here are my relationships in schema.prisma这是我在 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.当我使用 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.
I can't assign owner because the const holding it is not wrapped up until this command is done.我无法分配所有者,因为在完成此命令之前,持有它的 const 不会结束。 I also tried adding a random user as owenr and get this error.
我还尝试添加一个随机用户作为 owenr 并得到这个错误。
(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) |
对象字面量只能指定已知属性,并且 'userAvatar' 不存在于类型 '(Without<UserCreateInput, UserUncheckedCreateInput> & UserUncheckedCreateInput) | (Without<...> & UserCreateInput)'.ts(2322)
(没有<...> & UserCreateInput)'.ts(2322)
Your User
model does not have a userAvatar
field defined, only avatar
.您的
User
模型没有定义userAvatar
字段,只有avatar
。 If you are going to create an Upload
model as part of seeding the database, you have to use fields Prisma knows about.如果你打算创建一个
Upload
模型作为数据库种子的一部分,你必须使用 Prisma 知道的字段。 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:它不知道
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,
},
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.