3

The goal is simple, I want to query enum field in the where clause. Example

prisma version is: "prisma": "3.11.0"

schema.prisma

model Todo {
  id Int @default(autoincrement()) @id
  title String
  content String
  status TodoStatus @default(IN_PROGRESS)
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

enum TodoStatus {
  IN_PROGRESS 
  DONE
}

api call

app.get('/todos', (req, res){
   const { status } = req.query
   const todos = await prisma.todo.findMany({
      where: { status: status },
      orderBy: {
        id: "asc",
      },
    });

    res.json(todos);
})

Frontend I use Next.js and it is a select option

example

            <select
              id="todo"
              name="todo"
              onChange={(e) => setSelectedStatus(e.target.value as Status)}
            >
              {[Status.IN_PROGRESS, Status.DONE].map((status: Status) => {
                return (
                  <option value={status}>{todoStatusToString[status]}</option>
                );
              })}
            </select>

The enum value from Next.js

export enum Status {
  IN_PROGRESS = "IN_PROGRESS",
  DONE = "DONE",
  ALL = "ALL",
}

export const todoStatusToString = {
  [Status.IN_PROGRESS]: "In progress",
  [Status.DONE]: "Done",
  [Status.ALL]: "All",
};

the req.query will be sent from clientside in this format

localhost:3000/todos?status=DONE

{ status: "DONE" }

or localhost:3000/todos?status=IN_PROGRESS

{ status: "IN_PROGRESS" } 

I know that Prisma is built-in to be typesafe. So my assumption is because the data that we get from the frontend is a string type while the enum on Prisma is looking for either IN_PROGRESS or DONE specifically if we send "DECLINED" to status where clause it will spit the same error.

Any help would be appreciated!

enter image description here

3
  • Passing DONE or IN_PROGRESS as string should work. I just tried it out and it worked for me. What version of Prisma and Database are you using? I tried it and it worked in PostgreSQL and prisma client verison of 3.7.0 Commented Mar 15, 2022 at 11:31
  • "@prisma/client": "3.10.0" this is my prisma version. Commented Mar 16, 2022 at 2:51
  • and im using postgreSQL Commented Mar 16, 2022 at 2:52

3 Answers 3

0

In this case you need to cast the type. You can do it like this

where: { status: status as unknown as TodoStatus}
Sign up to request clarification or add additional context in comments.

Comments

0

David answer works, But it is better to validate req.query using ClassValidator or Zod and set the type inside the validator as TodoStatus instead of normal string.

Comments

0

I had a similar issue. In my case the enums are called Role in the schema rather than TodoStatus. My error:

Type 'string' is not assignable to type 'Role | EnumRoleFilter | undefined'.ts(2322)
(property) role?: Role | Prisma.EnumRoleFilter | undefined

Code that caused the error:

const dummyOwnerUser = {
    ...
    role: "OWNER"
}
let ownerUser = await client.user.findFirst({
    where: {
        ...
        role: dummyOwnerUser.role
    }
})

No error:

const dummyOwnerUser = {
    ...
    role: "OWNER" as RoleType
}
let ownerUser = await client.user.findFirst({
    where: {
        ...
        role: dummyOwnerUser.role
    }
})

So presumably OP needed to cast status as a TodoStatusType prior to making the query.

Edit:

It's probably quite important to mention I'm using zod-prisma-types from npm!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.