英文:
Using `disconnect` to remove relation doesn't type check
问题
我有以下的Prisma模型定义:
model 宠物 {
id Int @id @default(autoincrement())
名字 String
种类 String
主人 主人 @relation(fields: [主人Id], references: [id])
主人Id Int
}
model 主人 {
id Int @id @default(autoincrement())
名字 String
年龄 Int
宠物们 宠物[]
}
我尝试编写一个查询,使用以下代码将宠物与主人断开关系:
await prisma.宠物.update({
where: {
id: 1
},
data: {
主人: {
断开关系: true
}
}
});
如文档此处所述,这应该会断开关系。
然而,主人
的断开关系
属性似乎存在类型错误。类型错误如下:
类型 '{ 断开关系: true; }' 无法分配给类型 'OwnerUpdateOneRequiredWithoutPetsNestedInput'。
对象文字只能指定已知属性,但'断开关系'不存在于类型'OwnerUpdateOneRequiredWithoutPetsNestedInput'中。您是否想写'连接'?ts(2322)
我尝试将主人
设置为undefined
,但这似乎不会以任何方式更新数据库(它确实进行了类型检查)。我还尝试将主人Id
设置为undefined
,它可以运行和检查,但同样数据库似乎不会更新。
我使用的是SQLite和Prisma Studio。Prisma和Prisma客户端版本为14.4,Typescript版本为5.0。
如何修复这个问题?
英文:
I have the following Prisma model definitions:
model Pet {
id Int @id @default(autoincrement())
name String
kind String
owner Owner @relation(fields: [ownerId], references: [id])
ownerId Int
}
model Owner {
id Int @id @default(autoincrement())
name String
age Int
pets Pet[]
}
I'm trying to write a query that disconnects a Pet from an Owner using the following code:
await prisma.pet.update({
where: {
id: 1
},
data: {
owner: {
disconnect: true
}
}
});
As stated in the docs here, this should disconnect the relation.
However, there seems to be a type error on the disconnect
property of owner
. The type error:
Type '{ disconnect: true; }' is not assignable to type 'OwnerUpdateOneRequiredWithoutPetsNestedInput'.
Object literal may only specify known properties, but 'disconnect' does not exist in type 'OwnerUpdateOneRequiredWithoutPetsNestedInput'. Did you mean to write 'connect'?ts(2322)
I have tried setting owner
to undefined
but that does not seem to update the database in any way (it does typecheck). I also tried setting ownerId
to undefined
and it runs and check but again the database doesn't seem to update.
I'm using SQLite and Prisma Studio. Prisma & Prisma client version 14.4 and Typescript 5.0.
How would I fix this?
答案1
得分: 2
在你的情况下,Owner
和 Pet
之间存在一对多的关系,其中一个 Owner 可以拥有多只宠物,但一只宠物只能有一个 Owner。
当你尝试使用以下代码断开一个 Pet 与 Owner 的关系时:
await prisma.pet.update({
where: {
id: 1
},
data: {
owner: {
disconnect: true
}
}
});
你会得到一个类型错误,因为在这个上下文中 disconnect 属性是无效的。
在一对多关系中,断开连接操作仅适用于关系的“多”一侧。你可以通过在 Owner 模型上操作来断开与 Owner 的关系,而不是 Pet 模型。
这个查询应该按照你的期望工作:
await prisma.owner.update({
where: {
id: ownerId // 拥有宠物的所有者的 id
},
data: {
pets: {
disconnect: {
id: 1 // 你要断开连接的宠物的 id
}
}
}
});
但请注意,上面的查询只有在你的模式允许 ownerId 字段可为空时才会起作用。如果不允许,你需要更新 Prisma 模式,通过添加一个 (?
) 使 ownerId 字段可为空。
模式可能如下所示:
model Pet {
id Int @id @default(autoincrement())
name String
kind String
owner Owner? @relation(fields: [ownerId], references: [id])
ownerId Int?
}
model Owner {
id Int @id @default(autoincrement())
name String
age Int
pets Pet[]
}
英文:
In your case, you have a one-to-many relationship between Owner
and Pet
, where an Owner can have many Pets, but a Pet can have only one Owner.
When you try to disconnect a Pet from an Owner using the following code:
await prisma.pet.update({
where: {
id: 1
},
data: {
owner: {
disconnect: true
}
}
});
you're getting a type error because the disconnect property is not valid in this context.
In a one-to-many relationship, the disconnect operation is only applicable on the "many" side of the relation. You can disconnect a Pet from an Owner by operating on the Owner model, not the Pet model.
This query should work as you expect:
await prisma.owner.update({
where: {
id: ownerId // the id of the owner who owns the pet
},
data: {
pets: {
disconnect: {
id: 1 // the id of the pet you want to disconnect
}
}
}
});
But please note that the above query will only work if your schema allows for the ownerId field to be nullable. If it doesn't, you'll need to update your Prisma schema to make the ownerId field nullable by adding a (?
)
The schema would look something like this:
model Pet {
id Int @id @default(autoincrement())
name String
kind String
owner Owner? @relation(fields: [ownerId], references: [id])
ownerId Int?
}
model Owner {
id Int @id @default(autoincrement())
name String
age Int
pets Pet[]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论