Skip to content

Panic calling findOne with mapped unique key fields #2353

@TLadd

Description

@TLadd

Bug description

I have a unique key comprised of two mapped fields like this (full schema provided below):

  pageId   String  @map("page_id")
  readerId String? @map("reader_id")

  @@unique([pageId, readerId], name: "bookmark_page_id_reader_id_key")

When I try to query using this unique key in findOne, the prisma client panics and crashes.

How to reproduce

I put together a reproduction in this repo: https://github.com/TLadd/prisma-unique-composite-key-query-bug. It has the prisma schema and a script that triggers the panic condition. But I think any unique key comprised of mapped keys like above should trigger it when trying to query with it. This is the query I am running:

  const result = await prisma.bookmark.findOne({
    where: {
      bookmark_page_id_reader_id_key: {
        pageId: "bb60cc30-c892-4b7e-820c-db91382e82ea",
        readerId: "55a56470-d026-4c42-a24b-30b692b747e2",
      },
    },
  });

Expected behavior

The query does not crash Prisma.

Prisma information

generator client {
  provider      = "prisma-client-js"
  binaryTargets = ["native", "debian-openssl-1.1.x"]
}

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

model Bookmark {
  id       String  @default(dbgenerated()) @id
  pageId   String  @map("page_id")
  readerId String? @map("reader_id")
  page     Page    @relation(fields: [pageId], references: [id])
  reader   Reader? @relation(fields: [readerId], references: [id])

  @@index([pageId], name: "bookmark_page_id_fkey")
  @@index([readerId], name: "bookmark_reader_id_fkey")
  @@map("bookmark")
  @@unique([pageId, readerId], name: "bookmark_page_id_reader_id_key")
}

model Page {
  id        String     @default(dbgenerated()) @id
  bookmarks Bookmark[]

  @@map("page")
}

model Reader {
  id        String     @default(dbgenerated()) @id
  bookmarks Bookmark[]

  @@map("reader")
}
  const result = await prisma.bookmark.findOne({
    where: {
      bookmark_page_id_reader_id_key: {
        pageId: "bb60cc30-c892-4b7e-820c-db91382e82ea",
        readerId: "55a56470-d026-4c42-a24b-30b692b747e2",
      },
    },
  });

Environment & setup

  • OS: Mac OS
  • Database: PostgreSQL
  • Prisma version: 2.0.0-beta.4
  • Node.js version: v12.16.2

Full debug output:

  prisma-client {
  prisma-client   engineConfig: {
  prisma-client     cwd: '/Users/thomasladd/github/prisma-map-foriegn-key/prisma',
  prisma-client     debug: false,
  prisma-client     datamodelPath: '/Users/thomasladd/github/prisma-map-foriegn-key/node_modules/.prisma/client/schema.prisma',
  prisma-client     prismaPath: undefined,
  prisma-client     datasources: [],
  prisma-client     generator: {
  prisma-client       name: 'client',
  prisma-client       provider: 'prisma-client-js',
  prisma-client       output: '/Users/thomasladd/github/prisma-map-foriegn-key/node_modules/@prisma/client',
  prisma-client       binaryTargets: [Array],
  prisma-client       config: {}
  prisma-client     },
  prisma-client     showColors: false,
  prisma-client     logLevel: 'info',
  prisma-client     logQueries: true,
  prisma-client     env: {
  prisma-client       DATABASE_URL: 'postgresql://user:pass@localhost:5432/db?schema=public'
  prisma-client     },
  prisma-client     flags: []
  prisma-client   }
  prisma-client } +0ms
  prisma-client Prisma Client call: +76ms
  prisma-client prisma.bookmark.findOne({
  prisma-client   where: {
  prisma-client     bookmark_page_id_reader_id_key: {
  prisma-client       pageId: 'bb60cc30-c892-4b7e-820c-db91382e82ea',
  prisma-client       readerId: '55a56470-d026-4c42-a24b-30b692b747e2'
  prisma-client     }
  prisma-client   }
  prisma-client }) +1ms
  prisma-client Generated request: +0ms
  prisma-client query {
  prisma-client   findOneBookmark(where: {
  prisma-client     bookmark_page_id_reader_id_key: {
  prisma-client       pageId: "bb60cc30-c892-4b7e-820c-db91382e82ea"
  prisma-client       readerId: "55a56470-d026-4c42-a24b-30b692b747e2"
  prisma-client     }
  prisma-client   }) {
  prisma-client     id
  prisma-client     pageId
  prisma-client     readerId
  prisma-client   }
  prisma-client }
  prisma-client  +0ms
  getos { platform: 'darwin', libssl: undefined } +0ms
  engine {
  engine   PRISMA_DML_PATH: '/Users/thomasladd/github/prisma-map-foriegn-key/node_modules/.prisma/client/schema.prisma',
  engine   PORT: '54095',
  engine   RUST_BACKTRACE: '1',
  engine   RUST_LOG: 'info',
  engine   LOG_QUERIES: 'true',
  engine   OVERWRITE_DATASOURCES: '[]'
  engine } +0ms
  engine { cwd: '/Users/thomasladd/github/prisma-map-foriegn-key/prisma' } +1ms
  engine {
  engine   dotPrismaPath: '/Users/thomasladd/github/prisma-map-foriegn-key/node_modules/.prisma/client/query-engine-darwin'
  engine } +0ms
  plusX Execution permissions of /Users/thomasladd/github/prisma-map-foriegn-key/node_modules/.prisma/client/query-engine-darwin are fine +0ms
  engine { flags: [ '--enable-raw-queries' ] } +1ms
  engine stderr Printing to stderr for debugging +16ms
  engine stderr Listening on 127.0.0.1:54095 +1ms
  engine stdout {
  timestamp: 'May 01 16:49:23.615',
  level: 'INFO',
  target: 'quaint::pooled',
  fields: { message: 'Starting a postgresql pool with 13 connections.' }
} +0ms
prisma:info  Starting a postgresql pool with 13 connections.
  engine stdout {
  timestamp: 'May 01 16:49:23.621',
  level: 'INFO',
  target: 'query_engine::server',
  fields: { message: 'Started http server on 127.0.0.1:54095' }
} +6ms
prisma:info  Started http server on 127.0.0.1:54095
  engine stdout {
  timestamp: 'May 01 16:49:23.639',
  level: 'ERROR',
  target: 'query_engine',
  fields: {
    message: 'PANIC',
    reason: 'called `Option::unwrap()` on a `None` value',
    file: 'query-engine/core/src/query_graph_builder/extractors/filters.rs',
    line: 279,
    column: 35
  }
} +17ms
  engine {
  engine   error: Error: socket hang up
  engine       at connResetException (internal/errors.js:608:14)
  engine       at Socket.socketOnEnd (_http_client.js:460:23)
  engine       at Socket.emit (events.js:322:22)
  engine       at Socket.EventEmitter.emit (domain.js:482:12)
  engine       at endReadableNT (_stream_readable.js:1187:12)
  engine       at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  engine     code: 'ECONNRESET'
  engine   }
  engine } +3ms
  printStack callsite Error:
    at Object.model [as Bookmark] (/Users/thomasladd/github/prisma-map-foriegn-key/node_modules/@prisma/client/src/runtime/getPrismaClient.ts:376:28)
    at Object.n.<computed> [as findOne] (/Users/thomasladd/github/prisma-map-foriegn-key/node_modules/@prisma/client/src/runtime/getPrismaClient.ts:511:33)
    at tryIt (/Users/thomasladd/github/prisma-map-foriegn-key/testQuery.ts:8:40)
    at Object.<anonymous> (/Users/thomasladd/github/prisma-map-foriegn-key/testQuery.ts:20:1)
    at Module._compile (internal/modules/cjs/loader.js:1156:30)
    at Module.m._compile (/Users/thomasladd/github/prisma-map-foriegn-key/node_modules/ts-node/src/index.ts:839:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/thomasladd/github/prisma-map-foriegn-key/node_modules/ts-node/src/index.ts:842:12)
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14) +0ms
(node:95010) UnhandledPromiseRejectionWarning: Error:
Invalid `prisma.bookmark.findOne()` invocation in
/Users/thomasladd/github/prisma-map-foriegn-key/testQuery.ts:8:40

   4   log: ["query", "info", "warn"],
   5 });
   6
   7 async function tryIt() {
→  8   const result = await prisma.bookmark.findOne(PANIC: called `Option::unwrap()` on a `None` value
    at PrismaClientFetcher.request (/Users/thomasladd/github/prisma-map-foriegn-key/node_modules/@prisma/client/src/runtime/getPrismaClient.ts:643:15)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:95010) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:95010) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Error in Prisma Client:
PANIC: called `Option::unwrap()` on a `None` value in
query-engine/core/src/query_graph_builder/extractors/filters.rs:279:35

This is a non-recoverable error which probably happens when the Prisma Query Engine has a panic.
Please create an issue in https://github.com/prisma/prisma-client-js describing the last Prisma Client query you called.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug/2-confirmedBug has been reproduced and confirmed.kind/bugA reported bug.tech/enginesIssue for tech Engines.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions