Home > other >  Prisma findUnique function
Prisma findUnique function

Time:11-11

Trying to use findUnique on my model project returns this error ->

Type '{ client_key: string; }' is not assignable to type 'ProjectWhereUniqueInput'.
  Object literal may only specify known properties, and 'client_key' does not exist in type 'ProjectWhereUniqueInput'

. I understand to use the findUnique I need to add the @unique identifier however Im stuck on this now. Heres the model and func call. Any help?

model Project {
  id              String       @id
  created         DateTime
  org             Organization @relation(fields: org_id, references: id)
  org_id          String
  content_title   String
  content_id      String?
  client_key      String       @unique
  has_data        Boolean
}
const project = await prisma.project.findUnique({
      where:{
        client_key: 'client_key',
      }
    });

CodePudding user response:

you can write:

const project = await prisma.project.findUnique({
  where:{
    client_key: project.client_key,
  }
});

CodePudding user response:

Considering this schema:

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["filteredRelationCount"]
}

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

model Project {
  id            String       @id
  created       DateTime
  org           Organization @relation(fields: org_id, references: id)
  org_id        String
  content_title String
  content_id    String?
  client_key    String       @unique
  has_data      Boolean
}

model Organization {
  id        String    @id
  name      String
  projects  Project[]
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt
}

And executing the below file:

import { PrismaClient, Prisma } from '@prisma/client';

const prisma = new PrismaClient({
  log: ['query', 'info', 'warn'],
});

async function main() {
  await prisma.project.create({
    data: {
      id: '1',
      client_key: '123',
      content_title: 'test',
      created: new Date(),
      has_data: true,
      content_id: '1',
      org: {
        create: {
          id: '1',
          name: 'test',
        },
      },
    },
  });

  const project = await prisma.project.findUnique({
    where: {
      client_key: '123',
    },
  });

  console.log(project);
}

main()
  .catch((e) => {
    throw e;
  })
  .finally(async () => {
    await prisma.$disconnect();
  });

Would display an accurate response.

> [email protected] dev
> ts-node index.ts

prisma:info Starting a postgresql pool with 0 connections.
prisma:query BEGIN
prisma:query INSERT INTO "public"."Organization" ("id","name","createdAt","updatedAt") VALUES ($1,$2,$3,$4) RETURNING "public"."Organization"."id"
prisma:query INSERT INTO "public"."Project" ("id","created","org_id","content_title","content_id","client_key","has_data") VALUES ($1,$2,$3,$4,$5,$6,$7) RETURNING "public"."Project"."id"
prisma:query SELECT "public"."Project"."id", "public"."Project"."created", "public"."Project"."org_id", "public"."Project"."content_title", "public"."Project"."content_id", "public"."Project"."client_key", "public"."Project"."has_data" FROM "public"."Project" WHERE "public"."Project"."id" = $1 LIMIT $2 OFFSET $3
prisma:query COMMIT
prisma:query SELECT "public"."Project"."id", "public"."Project"."created", "public"."Project"."org_id", "public"."Project"."content_title", "public"."Project"."content_id", "public"."Project"."client_key", "public"."Project"."has_data" FROM "public"."Project" WHERE ("public"."Project"."client_key" = $1 AND 1=1) LIMIT $2 OFFSET $3
{
  id: '1',
  created: 2022-11-11T08:59:14.216Z,
  org_id: '1',
  content_title: 'test',
  content_id: '1',
  client_key: '123',
  has_data: true
}

So the client_key is a valid parameter in findUnique query and should work. Please try executing npx prisma generate to confirm that schema file is in sync with PrismaClient

  • Related