尝试使用Prisma检查Postgres中的表中是否存在记录,但似乎只能查询id字段,而不能查询任何其他字段,如name
和location
,这会导致编译器错误
模型schema.prisma
model place {
id Int @id @default(dbgenerated("nextval('place_id_seq'::regclass)"))
name String
location String @unique
}
生成的类型
export type Place = {
__typename?: 'Place';
name?: Maybe<Scalars['String']>;
location?: Maybe<Scalars['String']>;
};
查询解析器
let findPlace = await prisma.place.findUnique(
{
where: {
name: "abc"
}
}
)
错误
Type '{ name: string; }' is not assignable to type 'placeWhereUniqueInput'.
Object literal may only specify known properties, and 'name' does not exist in type 'placeWhereUniqueInput'.ts(2322)
index.d.ts(1361, 5): The expected type comes from property 'where' which is declared here on type '{ select?: placeSelect | null | undefined; include?: placeInclude | null | undefined; rejectOnNotFound?: RejectOnNotFound | undefined; where: placeWhereUniqueInput; }'
这里缺少什么才能让它正常工作?
发布于 2021-08-17 02:04:28
Prisma不接受条件仅包含非唯一字段(在本例中为名称)的findUnique
查询。如果您只需要查看是否存在符合条件的place记录,可以使用count
API。
let placeCount = await prisma.place.count(
{
where: {
name: "abc"
}
}
)
// placeCount == 0 implies does not exist
https://stackoverflow.com/questions/68810116
复制相似问题