Skip to content

called Option::unwrap() on a None value when using the relationJoins preview feature with driver adapters #22294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mjyoung opened this issue Dec 6, 2023 · 9 comments
Labels
bug/2-confirmed Bug has been reproduced and confirmed. domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. kind/bug A reported bug. topic: driverAdapters topic: previewFeatures Issue touches on an preview feature flag topic: relationJoins
Milestone

Comments

@mjyoung
Copy link

mjyoung commented Dec 6, 2023

Bug description

I updated to 5.7.0 today and tried the preview features relationJoins and nativeDistinct. I'm also using driverAdapters with Neon DB.

I am attempting to do a prisma findFirst with some nested includes:

  const user = await db.user.findFirst({
    where: {
      id: 1,
    },
    include: {
      accountExecutives: {
        include: {
          vendor: true,
        },
      },
    },
  });

If I point the DB to my local, non-Neon DB everything works fine.

{
  "user": {
    "id": 1,
    ...
    "accountExecutives": [
      {
        "id": 1,
        ...
        "vendor": {
          "id": 1,
          ...
        }
      }
    ]
  }
}

However when I point it to my Neon DB I get the following error:

Error while processing the request
PrismaClientRustPanicError:
Invalid `prisma.user.findFirst()` invocation:


called `Option::unwrap()` on a `None` value

This is a non-recoverable error which probably happens when the Prisma Query Engine has a panic.
{"name":"PrismaClientRustPanicError","clientVersion":"5.7.0"}

How to reproduce

// schema.prisma

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

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["driverAdapters", "relationJoins", "nativeDistinct"]
}

model AccountExecutive {
  id         Int      @id @default(autoincrement())
  vendor     Vendor   @relation(fields: [vendorId], references: [id], onDelete: Cascade)
  vendorId   Int
  user       User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  userId     Int

  @@unique([userId, vendorId])
  @@index([userId])
  @@index([vendorId])
}

model User {
  id         Int      @id @default(autoincrement())
  accountExecutives   AccountExecutive[]
}

model Vendor {
  id         Int      @id @default(autoincrement())
  accountExecutives   AccountExecutive[]
}
// db.js

import { Pool, neonConfig } from '@neondatabase/serverless';
import { PrismaNeon } from '@prisma/adapter-neon';
import { PrismaClient } from '@prisma/client';
import ws from 'ws';

import { IS_DEV } from '~/app/core/config';

neonConfig.webSocketConstructor = ws;
const connectionString = `${process.env.DATABASE_URL}`;

const pool = new Pool({ connectionString });
const adapter = new PrismaNeon(pool);

const prismaClientSingleton = () => {
  return new PrismaClient({
    ...(!process.env.DATABASE_URL?.includes('127.0.0.1') && { adapter }),
    log: IS_DEV
      ? ['query', 'info', 'warn', 'error']
      : ['info', 'warn', 'error'],
  });
};

type PrismaClientSingleton = ReturnType<typeof prismaClientSingleton>;

const globalForPrisma = globalThis as unknown as {
  prisma: PrismaClientSingleton | undefined;
};

const prisma = globalForPrisma.prisma ?? prismaClientSingleton();

if (IS_DEV) globalForPrisma.prisma = prisma;

export * from '@prisma/client';

const db = prisma;
export default db;
// pages/api/prisma-570.ts

import { api } from '~/app/blitz-server';
import { APP_ENV } from '~/app/core/config';
import db from '~/db';

const handler = api(async (_req, res, ctx) => {
  if (APP_ENV !== 'development' && APP_ENV !== 'preview')
    return res.status(404).send('Not found');

  const user = await db.user.findFirst({
    where: {
      id: 1,
    },
    include: {
      accountExecutives: {
        include: {
          vendor: true,
        },
      },
    },
  });

  res.status(200).json({
    user: user,
  });
});

export default handler;

Environment & setup

  • OS: macOS
  • Database: PostgreSQL (hosted on Neon, using @prisma/adapter-neon and @neondatabase/serverless
  • Node.js version: 20

Prisma Version

prisma                  : 5.7.0
@prisma/client          : 5.7.0
Computed binaryTarget   : darwin-arm64
Operating System        : darwin
Architecture            : arm64
Node.js                 : v20.8.1
Query Engine (Node-API) : libquery-engine 79fb5193cf0a8fdbef536e4b4a159cad677ab1b9 (at node_modules/.pnpm/@prisma+engines@5.7.0/node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Schema Engine           : schema-engine-cli 79fb5193cf0a8fdbef536e4b4a159cad677ab1b9 (at node_modules/.pnpm/@prisma+engines@5.7.0/node_modules/@prisma/engines/schema-engine-darwin-arm64)
Schema Wasm             : @prisma/prisma-schema-wasm 5.7.0-41.79fb5193cf0a8fdbef536e4b4a159cad677ab1b9
Default Engines Hash    : 79fb5193cf0a8fdbef536e4b4a159cad677ab1b9
Studio                  : 0.495.0
Preview Features        : driverAdapters, nativeDistinct, relationJoins
@mjyoung mjyoung added the kind/bug A reported bug. label Dec 6, 2023
@mjyoung
Copy link
Author

mjyoung commented Dec 6, 2023

I just tried removing relationJoins and the queries work fine again. Looks like the Neon driver adapter and the new relationJoins preview feature aren't playing nicely together.

@mjyoung mjyoung changed the title called Option::unwrap() on a None value called Option::unwrap() on a None value when using the relationJoins preview feature Dec 6, 2023
@janpio janpio added topic: previewFeatures Issue touches on an preview feature flag topic: relationJoins bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. labels Dec 6, 2023
@janpio
Copy link
Contributor

janpio commented Dec 6, 2023

Thanks for the report, and all the included information - that should allow us to reproduce the problem.
Can you maybe still try to see if query logging logs something that could be helpful?

@janpio janpio added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. 5.7.0 and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Dec 6, 2023
@mjyoung
Copy link
Author

mjyoung commented Dec 6, 2023

@janpio Here's the full error with log level query and RUST_BACKTRACE=full:

 ✓ Compiled /api/sandbox/prisma in 205ms (1078 modules)
thread 'tokio-runtime-worker' panicked at query-engine/core/src/response_ir/internal.rs:427:55:
called `Option::unwrap()` on a `None` value
stack backtrace:
prisma:query SELECT "t1"."id", "t1"."firstName", "t1"."lastName", "t1"."email", "t1"."photoUrl", "t1"."hashedPassword", "t1"."apiKey", "t1"."role"::text, "t1"."createdAt", "t1"."updatedAt", "t1"."suspendedAt", "User_accountExecutives"."__prisma_data__" AS "accountExecutives" FROM "public"."User" AS "t1" LEFT JOIN LATERAL (SELECT COALESCE(JSON_AGG("__prisma_data__"), '[]') AS "__prisma_data__" FROM (SELECT "t4"."__prisma_data__" FROM (SELECT JSON_BUILD_OBJECT('id', "t3"."id", 'jobTitle', "t3"."jobTitle", 'vendorId', "t3"."vendorId", 'userId', "t3"."userId", 'numInvites', "t3"."numInvites", 'createdAt', "t3"."createdAt", 'updatedAt', "t3"."updatedAt", 'vendorId', "t3"."vendorId", 'vendor', "AccountExecutive_vendor"."__prisma_data__") AS "__prisma_data__", "t3"."vendorId" FROM (SELECT "t2".* FROM "public"."AccountExecutive" AS "t2" WHERE "t1"."i   0:        0x136ec8808 - _napi_register_module_v1
   1:        0x136805bb0 - <unknown>
   2:        0x136eaaa04 - _napi_register_module_v1
   3:        0x136ecbd94 - _napi_register_module_v1
   4:        0x136ecb9f8 - _napi_register_module_v1
   5:        0x136ecc860 - _napi_register_module_v1
   6:        0x136ecc3e0 - _napi_register_module_v1
   7:        0x136ecc370 - _napi_register_module_v1
   8:        0x136ecc364 - _napi_register_module_v1
   9:        0x137158910 - _napi_register_module_v1
  10:        0x137158a38 - _napi_register_module_v1
  11:        0x136c9751c - _napi_register_module_v1
  12d" = "t2"."userId" /* root select */) AS "t3" LEFT JOIN LATERAL (SELECT COALESCE(JSON_AGG("__prisma_data__"), '[]') AS "__prisma_data__" FROM (SELECT "t8"."__prisma_data__" FROM (SELECT JSON_BUILD_OBJECT('id', "t7"."id", 'name', "t7"."name", 'emailDomain', "t7"."emailDomain", 'websiteUrl', "t7"."websiteUrl", 'logoUrl', "t7"."logoUrl", 'createdAt', "t7"."createdAt", 'updatedAt', "t7"."updatedAt") :        0x136c97100 - _napi_register_module_v1
  13:        0x136c97ea8 - _napi_register_module_v1
  14:        0x136cd0cd8 - _napi_register_module_v1
  15:        0x136cd0224 - _napi_register_module_v1
  16:        0x136ccf2d4 - _napi_register_module_v1
  17:        0x13670edfc - <unknown>
  18:        0x136710380 - <unknown>
  19:        0x13672e264 - <unknown>
  20:        0x136729f38 - <unknown>
  21:        0x136717600 - <unknown>
  22:        0x136775104 - <unknown>
  23:        0x13674f75c - <unknown>
  24:        0x1367590bc - <unknown>
  25:        0x136ee2aa8 - _napi_register_module_v1
  26:        0x136eee7bc - _napi_register_module_v1
  27:        0x136ee1474 - _napi_register_module_v1
  28:        0x136ee0f80 - _napi_register_module_v1
  29:        0x136ee7eac - _napi_register_module_v1
  30:        0x136ecd938 - _napi_register_module_v1
  31:        0x199ae3fa8 - __pthread_joiner_wake
AS "__prisma_data__" FROM (SELECT "t6".* FROM "public"."Vendor" AS "t6" WHERE "t3"."vendorId" = "t6"."id" /* root select */) AS "t7" /* inner select */) AS "t8" /* middle select */) AS "t9" /* outer select */) AS "AccountExecutive_vendor" ON true /* inner select */) AS "t4" /* middle select */) AS "t5" /* outer select */) AS "User_accountExecutives" ON true WHERE ("t1"."id") IN (SELECT "t1"."userId" FROM "public"."AccountExecutive" AS "t1" WHERE (1=1 AND "t1"."userId" IS NOT NULL)) LIMIT $1
prisma:error
Invalid `prisma.user.findFirst()` invocation:


called `Option::unwrap()` on a `None` value

This is a non-recoverable error which probably happens when the Prisma Query Engine has a panic.

https://github.com/prisma/prisma/issues/new?body=Hi+Prisma+Team%21+My+Prisma+Client+just+crashed.+This+is+the+report%3A%0A%23%23+Versions%0A%0A%7C+Name++++++++++++%7C+Version++++++++++++%7C%0A%7C-----------------%7C--------------------%7C%0A%7C+Node++++++++++++%7C+v20.8.1++++++++++++%7C+%0A%7C+OS++++++++++++++%7C+darwin-arm64+++++++%7C%0A%7C+Prisma+Client+++%7C+5.7.0++++++++++++++%7C%0A%7C+Query+Engine++++%7C+79fb5193cf0a8fdbef536e4b4a159cad677ab1b9%7C%0A%7C+Database++++++++%7C+postgresql+++++++++%7C%0A%0A%0A%0A%23%23+Logs%0A%60%60%60%0Aprisma%3AtryLoadEnv+Environment+variables+not+found+at+null%0Aprisma%3AtryLoadEnv+Environment+variables+not+found+at+undefined%0Aprisma%3AtryLoadEnv+No+Environment+variables+loaded%0Aprisma%3Aclient+checkPlatformCaching%3Apostinstall+false%0Aprisma%3Aclient+checkPlatformCaching%3AciName+%0Aprisma%3Aclient+dirname+%2FUsers%2Fmichaelyoung%2FDesktop%2Fprojects%2Fromeano%2Fmonorepo%2Fromeano%2Fnode_modules%2F.pnpm%2F%40prisma%2Bclient%405.7.0_prisma%405.7.0%2Fnode_modules%2F.prisma%2Fclient%0Aprisma%3Aclient+relativePath+..%2F..%2F..%2F..%2F..%2F..%2Fdb%0Aprisma%3Aclient+cwd+%2FUsers%2Fmichaelyoung%2FDesktop%2Fprojects%2Fromeano%2Fmonorepo%2Fromeano%2Fdb%0Aprisma%3Aclient+clientVersion+5.7.0%0Aprisma%3Aclient%3AlibraryEngine+internalSetup%0Aprisma%3Aclient%3Aengines%3AresolveEnginePath+enginePath+%2FUsers%2Fmichaelyoung%2FDesktop%2Fprojects%2Fromeano%2Fmonorepo%2Fromeano%2Fnode_modules%2F.pnpm%2F%40prisma%2Bclient%405.7.0_prisma%405.7.0%2Fnode_modules%2F.prisma%2Fclient%2Flibquery_engine-darwin-arm64.dylib.node%0Aprisma%3Aclient%3AlibraryEngine+Using+driver+adapter%3A+%25O+%7B%22errorRegistry%22%3A%7B%22registeredErrors%22%3A%5B%5D%7D%2C%22provider%22%3A%22postgres%22%7D%0Aprisma%3Aclient%3AlibraryEngine+sending+request%2C+this.libraryStarted%3A+false%0Aprisma%3Aclient%3AlibraryEngine+library+starting%0Aprisma%3Aclient%3AlibraryEngine+library+started%0A%60%60%60%0A%0A%23%23+Client+Snippet%0A%60%60%60ts%0A%2F%2F+PLEASE+FILL+YOUR+CODE+SNIPPET+HERE%0A%60%60%60%0A%0A%23%23+Schema%0A%60%60%60prisma%0A%2F%2F+PLEASE+ADD+YOUR+SCHEMA+HERE+IF+POSSIBLE%0A%60%60%60%0A%0A%23%23+Prisma+Engine+Query%0A%60%60%60%0A%7B%22X%22%3Atrue%7D%7D%7D%7D%7D%0A%60%60%60%0A&title=called+%60Option%3A%3Aunwrap%28%29%60+on+a+%60None%60+value&template=bug_report.yml

If you want the Prisma team to look into it, please open the link above 🙏
To increase the chance of success, please post your schema and a snippet of
how you used Prisma Client in the issue.

 ⨯ PrismaClientRustPanicError:
Invalid `prisma.user.findFirst()` invocation:


called `Option::unwrap()` on a `None` value

This is a non-recoverable error which probably happens when the Prisma Query Engine has a panic.

https://github.com/prisma/prisma/issues/new?body=Hi+Prisma+Team%21+My+Prisma+Client+just+crashed.+This+is+the+report%3A%0A%23%23+Versions%0A%0A%7C+Name++++++++++++%7C+Version++++++++++++%7C%0A%7C-----------------%7C--------------------%7C%0A%7C+Node++++++++++++%7C+v20.8.1++++++++++++%7C+%0A%7C+OS++++++++++++++%7C+darwin-arm64+++++++%7C%0A%7C+Prisma+Client+++%7C+5.7.0++++++++++++++%7C%0A%7C+Query+Engine++++%7C+79fb5193cf0a8fdbef536e4b4a159cad677ab1b9%7C%0A%7C+Database++++++++%7C+postgresql+++++++++%7C%0A%0A%0A%0A%23%23+Logs%0A%60%60%60%0Aprisma%3AtryLoadEnv+Environment+variables+not+found+at+null%0Aprisma%3AtryLoadEnv+Environment+variables+not+found+at+undefined%0Aprisma%3AtryLoadEnv+No+Environment+variables+loaded%0Aprisma%3Aclient+checkPlatformCaching%3Apostinstall+false%0Aprisma%3Aclient+checkPlatformCaching%3AciName+%0Aprisma%3Aclient+dirname+%2FUsers%2Fmichaelyoung%2FDesktop%2Fprojects%2Fromeano%2Fmonorepo%2Fromeano%2Fnode_modules%2F.pnpm%2F%40prisma%2Bclient%405.7.0_prisma%405.7.0%2Fnode_modules%2F.prisma%2Fclient%0Aprisma%3Aclient+relativePath+..%2F..%2F..%2F..%2F..%2F..%2Fdb%0Aprisma%3Aclient+cwd+%2FUsers%2Fmichaelyoung%2FDesktop%2Fprojects%2Fromeano%2Fmonorepo%2Fromeano%2Fdb%0Aprisma%3Aclient+clientVersion+5.7.0%0Aprisma%3Aclient%3AlibraryEngine+internalSetup%0Aprisma%3Aclient%3Aengines%3AresolveEnginePath+enginePath+%2FUsers%2Fmichaelyoung%2FDesktop%2Fprojects%2Fromeano%2Fmonorepo%2Fromeano%2Fnode_modules%2F.pnpm%2F%40prisma%2Bclient%405.7.0_prisma%405.7.0%2Fnode_modules%2F.prisma%2Fclient%2Flibquery_engine-darwin-arm64.dylib.node%0Aprisma%3Aclient%3AlibraryEngine+Using+driver+adapter%3A+%25O+%7B%22errorRegistry%22%3A%7B%22registeredErrors%22%3A%5B%5D%7D%2C%22provider%22%3A%22postgres%22%7D%0Aprisma%3Aclient%3AlibraryEngine+sending+request%2C+this.libraryStarted%3A+false%0Aprisma%3Aclient%3AlibraryEngine+library+starting%0Aprisma%3Aclient%3AlibraryEngine+library+started%0A%60%60%60%0A%0A%23%23+Client+Snippet%0A%60%60%60ts%0A%2F%2F+PLEASE+FILL+YOUR+CODE+SNIPPET+HERE%0A%60%60%60%0A%0A%23%23+Schema%0A%60%60%60prisma%0A%2F%2F+PLEASE+ADD+YOUR+SCHEMA+HERE+IF+POSSIBLE%0A%60%60%60%0A%0A%23%23+Prisma+Engine+Query%0A%60%60%60%0A%7B%22X%22%3Atrue%7D%7D%7D%7D%7D%0A%60%60%60%0A&title=called+%60Option%3A%3Aunwrap%28%29%60+on+a+%60None%60+value&template=bug_report.yml

If you want the Prisma team to look into it, please open the link above 🙏
To increase the chance of success, please post your schema and a snippet of
how you used Prisma Client in the issue.

    at si.handleRequestError (/Users/michaelyoung/Desktop/test/node_modules/.pnpm/@prisma+client@5.7.0_prisma@5.7.0/node_modules/@prisma/client/runtime/library.js:125:7178)
    at si.handleAndLogRequestError (/Users/michaelyoung/Desktop/test/node_modules/.pnpm/@prisma+client@5.7.0_prisma@5.7.0/node_modules/@prisma/client/runtime/library.js:125:6151)
    at si.request (/Users/michaelyoung/Desktop/test/node_modules/.pnpm/@prisma+client@5.7.0_prisma@5.7.0/node_modules/@prisma/client/runtime/library.js:125:5859)
    at async l (/Users/michaelyoung/Desktop/test/node_modules/.pnpm/@prisma+client@5.7.0_prisma@5.7.0/node_modules/@prisma/client/runtime/library.js:130:10025)
    at async handler (webpack-internal:///(api)/./pages/api/sandbox/prisma.ts:14:18) {
  clientVersion: '5.7.0',
  page: '/api/sandbox/prisma'
}

@janpio
Copy link
Contributor

janpio commented Dec 6, 2023

Thank you!

@controversial
Copy link

I just ran into this issue as well trying out relationJoins with driverAdapters.

My query looks like:

const result = await prisma.matchboxEvent.findFirst({
  where: { college_tradition_event: { subdomain } },
  include: { college_tradition_event: true },
});

My schema looks like:

model MatchboxEvent {
  id String @id @default(uuid())
  college_tradition_event CollegeTraditionEventMeta?
  // ...
}

model CollegeTraditionEventMeta {
  matchbox_event MatchboxEvent @relation(fields: [matchbox_event_id], references: [id])
  matchbox_event_id String @unique
  subdomain String? @unique
  // ...
}
Trace
    at si.handleRequestError (./src/prisma/generated/client/runtime/library.js:7519:41)
    at si.handleAndLogRequestError (./src/prisma/generated/client/runtime/library.js:7470:18)
    at si.request (./src/prisma/generated/client/runtime/library.js:7451:18)
    at async l1 (./src/prisma/generated/client/runtime/library.js:8031:26)
    at async eval (./src/helpers/db.ts:62:20)
digest: "1789810191"
thread 'tokio-runtime-worker' panicked at query-engine/core/src/response_ir/internal.rs:427:55:
called `Option::unwrap()` on a `None` value
prisma:query SELECT "t1"."id", "t1"."name", "t1"."slug", "t1"."created_at", "t1"."participantCap", "t1"."closeTime", "t1"."open_time", "t1"."admin_id", "t1"."match_ids", "MatchboxEvent_college_tradition_event"."__prisma_data__" AS "college_tradition_event" FROM "public"."MatchboxEvent" AS "t1" LEFT JOIN "public"."CollegeTraditionEventMeta" AS "j1" ON ("j1"."matchbox_event_id") = ("t1"."id") LEFT JOIN LATERAL (SELECT COALESCE(JSON_AGG("__prisma_data__"), '[]') AS "__prisma_data__" FROM (SELECT "t4"."__prisma_data__" FROM (SELECT JSON_BUILD_OBJECT('matchbox_event_id', "t3"."matchbox_event_id", 'school_name', "t3"."school_name", 'subdomain', "t3"."subdomain", 'email_exts', "t3"."email_exts", 'school_color', "t3"."school_color", 'student_body_count', "t3"."student_body_count", 'school_id', "t3"."school_id") AS "__prisma_data__" FROM (SELECT "t2".* FROM "public"."CollegeTraditionEventMeta" AS "t2" WHERE "t1"."id" = "t2"."matchbox_event_id" /* root select */) AS "t3" /* inner select */) AS "t4" /* middle select */) AS "t5" /* outer select */) AS "MatchboxEvent_college_tradition_event" ON true WHERE ("j1"."subdomain" = $1 AND ("j1"."matchbox_event_id" IS NOT NULL)) LIMIT $2
stack backtrace:
   0:        0x12cec8808 - _napi_register_module_v1
   1:        0x12c805bb0 - 
   2:        0x12ceaaa04 - _napi_register_module_v1
   3:        0x12cecbd94 - _napi_register_module_v1
   4:        0x12cecb9f8 - _napi_register_module_v1
   5:        0x12cecc860 - _napi_register_module_v1
   6:        0x12cecc3e0 - _napi_register_module_v1
   7:        0x12cecc370 - _napi_register_module_v1
   8:        0x12cecc364 - _napi_register_module_v1
   9:        0x12d158910 - _napi_register_module_v1
  10:        0x12d158a38 - _napi_register_module_v1
  11:        0x12cc9751c - _napi_register_module_v1
  12:        0x12ccd0d9c - _napi_register_module_v1
  13:        0x12ccd0224 - _napi_register_module_v1
  14:        0x12cccf2d4 - _napi_register_module_v1
  15:        0x12c70edfc - 
  16:        0x12c710380 - 
  17:        0x12c72e264 - 
  18:        0x12c729f38 - 
  19:        0x12c717600 - 
  20:        0x12c775104 - 
  21:        0x12c74f75c - 
  22:        0x12c7590bc - 
  23:        0x12cee2aa8 - _napi_register_module_v1
  24:        0x12ceee7bc - _napi_register_module_v1
  25:        0x12cee1474 - _napi_register_module_v1
  26:        0x12cee0f80 - _napi_register_module_v1
  27:        0x12cee7eac - _napi_register_module_v1
  28:        0x12cecd938 - _napi_register_module_v1
  29:        0x18cea9034 - __pthread_joiner_wake
prisma:error
Invalid `prisma.matchboxEvent.findFirst()` invocation:

called Option::unwrap() on a None value

This is a non-recoverable error which probably happens when the Prisma Query Engine has a panic.

https://github.com/prisma/prisma/issues/new?body=Hi+Prisma+Team%21+My+Prisma+Client+just+crashed.+This+is+the+report%3A%0A%23%23+Versions%0A%0A%7C+Name++++++++++++%7C+Version++++++++++++%7C%0A%7C-----------------%7C--------------------%7C%0A%7C+Node++++++++++++%7C+v20.2.0++++++++++++%7C+%0A%7C+OS++++++++++++++%7C+darwin-arm64+++++++%7C%0A%7C+Prisma+Client+++%7C+5.7.0++++++++++++++%7C%0A%7C+Query+Engine++++%7C+79fb5193cf0a8fdbef536e4b4a159cad677ab1b9%7C%0A%7C+Database++++++++%7C+postgresql+++++++++%7C%0A%0A%0A%0A%23%23+Logs%0A%60%60%60%0Aprisma%3AtryLoadEnv+Environment+variables+loaded+from+%2FUsers%2Fluke%2FDeveloper%2Fmarriagepact%2Fmarriagepact.com%2Fpackages%2Fmatchbox.marriagepact.com%2F.env%0Aprisma%3Aclient+checkPlatformCaching%3Apostinstall+false%0Aprisma%3Aclient+checkPlatformCaching%3AciName+%0Aprisma%3Aclient+dirname+src%2Fprisma%2Fgenerated%2Fclient%0Aprisma%3Aclient+relativePath+..%2F..%0Aprisma%3Aclient+cwd+%2FUsers%2Fluke%2FDeveloper%2Fmarriagepact%2Fmarriagepact.com%2Fpackages%2Fmatchbox.marriagepact.com%2Fsrc%2Fprisma%0Aprisma%3Aclient+clientVersion+5.7.0%0Aprisma%3Aclient%3AlibraryEngine+internalSetup%0Aprisma%3Aclient%3Aengines%3AresolveEnginePath+enginePath+src%2Fprisma%2Fgenerated%2Fclient%2Flibquery_engine-darwin-arm64.dylib.node%0Aprisma%3Aclient%3AlibraryEngine+Using+driver+adapter%3A+%25O+%7B%22errorRegistry%22%3A%7B%22registeredErrors%22%3A%5B%5D%7D%2C%22provider%22%3A%22postgres%22%7D%0Aprisma%3Aclient%3AlibraryEngine+sending+request%2C+this.libraryStarted%3A+false%0Aprisma%3Aclient%3AlibraryEngine+library+starting%0Aprisma%3Aclient%3AlibraryEngine+library+started%0Aprisma%3Aclient%3Arequest_handler+%7B%22name%22%3A%22PrismaClientRustPanicError%22%2C%22clientVersion%22%3A%225.7.0%22%7D%0Aprisma%3Aclient%3AlibraryEngine+sending+request%2C+this.libraryStarted%3A+true%0Aprisma%3Aclient%3Arequest_handler+%7B%22name%22%3A%22PrismaClientRustPanicError%22%2C%22clientVersion%22%3A%225.7.0%22%7D%0Aprisma%3Aclient%3AlibraryEngine+sending+request%2C+this.libraryStarted%3A+true%0Aprisma%3Aclient%3AlibraryEngine+sending+request%2C+this.libraryStarted%3A+true%0Aprisma%3Aclient%3AlibraryEngine+sending+request%2C+this.libraryStarted%3A+true%0Aprisma%3Aclient%3AlibraryEngine+sending+request%2C+this.libraryStarted%3A+true%0Aprisma%3Aclient%3Arequest_handler+%7B%22name%22%3A%22PrismaClientRustPanicError%22%2C%22clientVersion%22%3A%225.7.0%22%7D%0Aprisma%3Aclient%3AlibraryEngine+sending+request%2C+this.libraryStarted%3A+true%0A%60%60%60%0A%0A%23%23+Client+Snippet%0A%60%60%60ts%0A%2F%2F+PLEASE+FILL+YOUR+CODE+SNIPPET+HERE%0A%60%60%60%0A%0A%23%23+Schema%0A%60%60%60prisma%0A%2F%2F+PLEASE+ADD+YOUR+SCHEMA+HERE+IF+POSSIBLE%0A%60%60%60%0A%0A%23%23+Prisma+Engine+Query%0A%60%60%60%0A%7B%22X%22%3Atrue%7D%7D%7D%0A%60%60%60%0A&title=called+%60Option%3A%3Aunwrap%28%29%60+on+a+%60None%60+value&template=bug_report.yml

Versions
prisma                  : 5.7.0
@prisma/client          : 5.7.0
Computed binaryTarget   : darwin-arm64
Operating System        : darwin
Architecture            : arm64
Node.js                 : v20.2.0
Query Engine (Node-API) : libquery-engine 79fb5193cf0a8fdbef536e4b4a159cad677ab1b9 (at ../../node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Schema Engine           : schema-engine-cli 79fb5193cf0a8fdbef536e4b4a159cad677ab1b9 (at ../../node_modules/@prisma/engines/schema-engine-darwin-arm64)
Schema Wasm             : @prisma/prisma-schema-wasm 5.7.0-41.79fb5193cf0a8fdbef536e4b4a159cad677ab1b9
Default Engines Hash    : 79fb5193cf0a8fdbef536e4b4a159cad677ab1b9
Studio                  : 0.495.0
Preview Features        : driverAdapters, relationJoins

@aqrln aqrln changed the title called Option::unwrap() on a None value when using the relationJoins preview feature called Option::unwrap() on a None value when using the relationJoins preview feature with driver adapters Dec 14, 2023
@andyjy
Copy link
Contributor

andyjy commented Jan 11, 2024

Same issue (called Option::unwrap() on a None value when using the relationJoins preview feature with driver adapters)

  • using latest Prisma 5.8.0
  • in my case Neon serverless driver
  • error occurred locally, and deployed to Vercel Edge using Prisma Edge early-access support

Fixed for me by swapping to the new join method relationLoadStrategy: "query" available in 5.8.0.

Only affected one query (findUnique, joining a single many:many relation table) - all my other queries work fine after adding the relationLoadStrategy to the affected query.

Will try see if I can identify what was unique about that table/query. Update: I've tried examining the table schema and can't identify anything unique about this table nor the data returned vs. other queries that work fine with relationJoins enabled 😢

@andyjy
Copy link
Contributor

andyjy commented Jan 12, 2024

Cause identified (at least in my case) - this error occurs when the included relation returns empty (null) fields - i.e. the generated Json column in the result contains things like [{.."field": null}]

The error goes away when I specify specific relation fields to return and omit the null fields, e.g.:

include: {
  relatedTable: {
     select: {
        nonNullField1: true,
        nonNullField2: true,
        ...
    }
}

Brooooooklyn added a commit to napi-rs/napi-rs that referenced this issue Feb 1, 2024
…1934)

Hi 👋🏻, Alberto from @prisma here.
We are users of the `serde-json` feature in [https://github.com/prisma/prisma-engines](https://github.com/prisma/prisma-engines/blob/23fdc5965b1e05fc54e5f26ed3de66776b93de64/Cargo.toml#L55-L60).

## The Problem

While investigating a [user-reported Prisma issue](prisma/prisma#22294), we noticed that napi.rs treats `null` values in `Object` like `undefined` ones.

However, `null` and `undefined` are semantically different in JavaScript:
- `undefined` indicates that a value is not set
- `null` indicates that a value has been explicitly set

This PR, which we tested internally, fixes the user's issue on Prisma, and hopefully provides value to the `napi-rs` project as a whole.

## Scenario

Consider [this scenario](https://github.com/prisma/prisma-engines/blob/dcb8cb9817af92fefaf0f95117dfb5dc19545a2c/query-engine/driver-adapters/src/napi/result.rs#L32-L33), effectively equivalent to:

```rust
let napi_env: napi::sys::napi_env = /* ... */;

let value: JsUnknown = object.get_named_property("value")?;

// napi.rs implements the `FromNapiValue` trait for `serde_json` if
// the `serde_json` feature is enabled (which we use already).
let json = serde_json::Value::from_napi_value(napi_env, value.raw())?;

Ok(Self::Ok(json.into()));
```

By looking at `napi.rs`' [`serde.rs` source file](https://github.com/napi-rs/napi-rs/blob/napi%402.12.4/crates/napi/src/bindgen_runtime/js_values/serde.rs), we [can see](https://github.com/napi-rs/napi-rs/blob/napi%402.12.4/crates/napi/src/bindgen_runtime/js_values/serde.rs#L104-L108) that "JSON subobjects" (`Map<String, serde_json::Value>`) are populated only if the value returned by `JsObject::get` is not `None`.

This implies that, given `napi-rs`'  [`Object::get` definition](https://github.com/napi-rs/napi-rs/blob/napi%402.12.4/crates/napi/src/bindgen_runtime/js_values/object.rs#L24-L44), `Object::get("key")` returns `None` both when the original object is

```js
{
  // "key" is `undefined`
}
```

and when the object is

```js
{
  "key": null
}
```

It just so happens that `prisma-engines` explicitly set a few `null` values explicitly for the features it offers, but these values are stripped away when we use the [napi-flavoured Prisma Query Engine](https://github.com/prisma/prisma-engines/tree/main/query-engine/query-engine-node-api).

---

I am available for further comments, clarifications, and refactorings. Have a good day!
@mjyoung
Copy link
Author

mjyoung commented Feb 7, 2024

@jkomyno I noticed some good progress in this PR. Is this slated for the next prisma release?

Looks like this was released as of napi@2.15.1

@aqrln
Copy link
Member

aqrln commented Feb 8, 2024

This should be fixed by napi-rs/napi-rs#1934 and prisma/prisma-engines#4687 and the fix will be released in the stable 5.10.0 release. You can also use prisma@dev and @prisma/client@dev to try it out.

I'll go ahead and close the issue, please let us know if the fix works for you or if you still face the problem!

@aqrln aqrln closed this as completed Feb 8, 2024
@aqrln aqrln added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Feb 8, 2024
@janpio janpio added this to the 5.10.0 milestone Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. kind/bug A reported bug. topic: driverAdapters topic: previewFeatures Issue touches on an preview feature flag topic: relationJoins
Projects
None yet
Development

No branches or pull requests

5 participants