首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用prisma graphql apollo检查记录是否存在

使用prisma graphql apollo检查记录是否存在
EN

Stack Overflow用户
提问于 2021-08-16 23:19:47
回答 1查看 1.7K关注 0票数 1

尝试使用Prisma检查Postgres中的表中是否存在记录,但似乎只能查询id字段,而不能查询任何其他字段,如namelocation,这会导致编译器错误

模型schema.prisma

代码语言:javascript
运行
复制
model place {
  id             Int              @id @default(dbgenerated("nextval('place_id_seq'::regclass)"))
  name           String
  location       String @unique
}

生成的类型

代码语言:javascript
运行
复制
export type Place = {
  __typename?: 'Place';
  name?: Maybe<Scalars['String']>;
  location?: Maybe<Scalars['String']>;

};

查询解析器

代码语言:javascript
运行
复制
let findPlace = await prisma.place.findUnique(
        {
          where: {
            name: "abc"
          }
        }
)

错误

代码语言:javascript
运行
复制
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; }'

这里缺少什么才能让它正常工作?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-17 02:04:28

Prisma不接受条件仅包含非唯一字段(在本例中为名称)的findUnique查询。如果您只需要查看是否存在符合条件的place记录,可以使用count API

代码语言:javascript
运行
复制
let placeCount = await prisma.place.count(
        {
          where: {
            name: "abc"
          }
        }
)
// placeCount == 0 implies does not exist
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68810116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档