prismaprisma-graphqlprisma2

prisma findUnique where takes only one unique argument


I ran into an issue where I need to check if a user exists by his username and email since both are unique fields in the database, but I got an error. Argument where of type UserWhereUniqueInput needs exactly one argument, but you provided username and email. Please choose one. so, is there a way to execute this query just once?. instead of running one for each like the following

const user = await prisma.user.findUnique({
        where: {
          username,
          email,
        },
      });

and not like this

const user = await prisma.user.findUnique({
        where: {
          username,
        },
      });

const user = await prisma.user.findUnique({
        where: {
          email,
        },
      });


Solution

  • I am not entirely certain, but prisma returns a JS object... So perhaps a query like this should work:

    const query = await prisma.user.findUnique({
            where: {
              user: user.username
            },
           select: {
             user: true,
             email: true
          }
          });
    

    I believe this should work, but it still only finds by one unique. I am not sure exactly why you would select user + email as both unique, as I would assume that one is tied to the other anyhow.

    I would take a look over here for further answers if my solution was not able to work: https://www.prisma.io/docs/concepts/components/prisma-client/crud#findfirst