简体   繁体   中英

prisma2: How to filter by a non required field in prisma.user.findMany()?

This is my prisma datamodel:

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

model User {
  id               Int      @default(autoincrement()) @id
  email            String   @unique
  name             String
  password         String
  emailVerified    Boolean  @default(false)
  emailToken       String?
  emailTokenExpiry Float?
  createdAt        DateTime @default(now())
  updatedAt        DateTime @updatedAt
}

here, emailToken is an optional field. but I want to filter by this field. something like this:

await prisma.user.findMany({where: {emailToken: emailToken}})

But, prisma allows only required fields to filter that. Is there any way to achieve this?

I think it is possible to filter by optional fields. for example, this snippet works for me.

const [user] = await prisma.user.findMany({
      where: {
        emailToken: args.emailToken,
        emailTokenExpiry: {
          gte: Date.now() - 3600000,
        },
      },
    });

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