question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

prisma validate error - thread '<unnamed>' panicked at 'called `Option::unwrap()` on a `None` value'

See original GitHub issue

Bug description

I have a self-referencing table model in my schema that looks like this:

model Folder {
  id                         String            @db.Char(30) @default(cuid()) @id
  name                   String            @db.MediumText
  childFolders          Folder[]          @relation("ChildToParentFolder")
  parentFolderId     String?           @db.Char(30)
  parentFolder        Folder?           @relation("ChildToParentFolder", fields: [parentFolderId], references: [id])
}

But when I run DEBUG="*" npx prisma validate I see

  prisma:loadEnv project root found at [...path omited...]\package.json +0ms
  prisma:tryLoadEnv Environment variables loaded from [...path omited...]\.env +0ms
[dotenv][DEBUG] did not match key and value when parsing line 1: # Environment variables declared in this file are automatically made available to Prisma.
[dotenv][DEBUG] did not match key and value when parsing line 2: # See the documentation for more detail: https://pris.ly/d/prisma-schema#using-environment-variables[dotenv][DEBUG] did not match key and value when parsing line 3:
[dotenv][DEBUG] did not match key and value when parsing line 4: # Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server and 
MongoDB (Preview).
[dotenv][DEBUG] did not match key and value when parsing line 5: # See the documentation for all the connection string options: https://pris.ly/d/connection-strings 
[dotenv][DEBUG] did not match key and value when parsing line 6:
[dotenv][DEBUG] did not match key and value when parsing line 12:
Environment variables loaded from .env
  prisma:engines binaries to download libquery-engine, migration-engine, introspection-engine, prisma-fmt +0ms
Prisma schema loaded from prisma\schema.prisma
  prisma:getDMMF Using CLI Query Engine (Node-API) at: [...path omited...]\node_modules\@prisma\engines\query_engine-windows.dll.node +0ms
thread '<unnamed>' panicked at 'called `Option::unwrap()` on a `None` value', libs\datamodel\connectors\dml\src\model.rs:290:47
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: SyntaxError: Unexpected token c in JSON at position 0
    at JSON.parse (<anonymous>)
    at getDmmfNodeAPI ([...path omited...]\node_modules\prisma\build\index.js:36492:28)
    at async getDMMF ([...path omited...]\node_modules\prisma\build\index.js:36471:17)
    at async Object.parse ([...path omited...]\node_modules\prisma\build\index.js:104072:5)
    at async main ([...path omited...]\node_modules\prisma\build\index.js:105393:18)

I tried RUST_BACKTRACE=1 to get more info but that spit out the same thing except with the suggestion to try RUST_BACKTRACE=full, so I tried that and get this:

$ RUST_BACKTRACE=full npx prisma validate
Environment variables loaded from .env
Prisma schema loaded from prisma\schema.prisma
thread '<unnamed>' panicked at 'called `Option::unwrap()` on a `None` value', libs\datamodel\connectors\dml\src\model.rs:290:47
stack backtrace:
   0:     0x7ffb3d4f042e - napi_register_module_v1
   1:     0x7ffb3d50f5ba - rust_eh_personality
   2:     0x7ffb3d4e8d78 - napi_register_module_v1
   3:     0x7ffb3d4f3146 - napi_register_module_v1
   4:     0x7ffb3d4f2c34 - napi_register_module_v1
   5:     0x7ffb3d4f37a5 - napi_register_module_v1
   6:     0x7ffb3d4f335f - napi_register_module_v1
   7:     0x7ffb3d4f0d77 - napi_register_module_v1
   8:     0x7ffb3d4f32e9 - napi_register_module_v1
   9:     0x7ffb3d6a00d0 - rust_eh_personality
  10:     0x7ffb3d6a001c - rust_eh_personality
  11:     0x7ffb3d019a5f - napi_register_module_v1
  12:     0x7ffb3ce5a436 - napi_register_module_v1
  13:     0x7ffb3cebef74 - napi_register_module_v1
  14:     0x7ffb3ce5826f - napi_register_module_v1
  15:     0x7ffb3c22644d - <unknown>
  16:     0x7ffb3c23d010 - <unknown>
  17:     0x7ffb3c25514b - <unknown>
  18:     0x7ff74de2aac7 - node::Stop
  19:     0x7ff74e67f11f - v8::internal::Builtins::builtin_handle
  20:     0x7ff74e67e6b4 - v8::internal::Builtins::builtin_handle
  21:     0x7ff74e67e9a8 - v8::internal::Builtins::builtin_handle
  22:     0x7ff74e67e7f3 - v8::internal::Builtins::builtin_handle
  23:     0x7ff74e75d67d - v8::internal::SetupIsolateDelegate::SetupHeap
  24:     0x7ff74e6f3762 - v8::internal::SetupIsolateDelegate::SetupHeap
  25:     0x7ff74e721810 - v8::internal::SetupIsolateDelegate::SetupHeap
  26:     0x7ff74e79fc6e - v8::internal::SetupIsolateDelegate::SetupHeap
  27:     0x7ff74e713a10 - v8::internal::SetupIsolateDelegate::SetupHeap
  28:     0x7ff74e6f130c - v8::internal::SetupIsolateDelegate::SetupHeap
  29:     0x7ff74e5c0b10 - v8::internal::Execution::CallWasm
  30:     0x7ff74e5c0c1b - v8::internal::Execution::CallWasm
  31:     0x7ff74e5c165a - v8::internal::Execution::TryCall
  32:     0x7ff74e5a1735 - v8::internal::MicrotaskQueue::RunMicrotasks
  33:     0x7ff74e5a1490 - v8::internal::MicrotaskQueue::PerformCheckpoint
  34:     0x7ff74de80904 - node::CallbackScope::~CallbackScope
  35:     0x7ff74de80d3b - node::CallbackScope::~CallbackScope
  36:     0x7ff74de789c4 - v8::internal::compiler::Operator::EffectOutputCount
  37:     0x7ff74dde5eb5 - SSL_get_quiet_shutdown
  38:     0x7ff74ddd933e - v8::base::CPU::has_sse
  39:     0x7ff74deb4e47 - uv_timer_stop
  40:     0x7ff74deb141b - uv_async_send
  41:     0x7ff74deb0bac - uv_loop_init
  42:     0x7ff74deb0d4a - uv_run
  43:     0x7ff74dda8a45 - v8::internal::AsmJsScanner::GetIdentifierString
  44:     0x7ff74de21227 - node::Start
  45:     0x7ff74dc7685c - RC4_options
  46:     0x7ff74ec31c08 - v8::internal::compiler::RepresentationChanger::Uint32OverflowOperatorFor
  47:     0x7ffb97cc7034 - BaseThreadInitThunk
  48:     0x7ffb97e02651 - RtlUserThreadStart
Error: Unexpected token c in JSON at position 0

How to reproduce

  1. Make self-referencing model in Prisma2 schema as shown above
  2. Run npx prisma validate
  3. become saddened by error

Expected behavior

Ideally, validation would check the file

Environment & setup

  • OS: Windows (above from within Git Bash terminal in VS Code)
  • Database: MySQL
  • Node.js version: v14.18.0

Prisma Version

prisma                  : 3.4.0
@prisma/client          : 3.4.0
Current platform        : windows
Query Engine (Node-API) : libquery-engine 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85 (at node_modules\@prisma\engines\query_engine-windows.dll.node)
Migration Engine        : migration-engine-cli 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85 (at node_modules\@prisma\engines\migration-engine-windows.exe)
Introspection Engine    : introspection-core 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85 (at node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary           : prisma-fmt 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85 (at node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash    : 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85
Studio                  : 0.438.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
janpiocommented, Nov 10, 2021

In Prisma 3.3.0 the validate and format comamnd gave a much more helpful error message:

λ npx prisma@3.3.0 format
Environment variables loaded from .env
Prisma schema loaded from prisma\schema.prisma
Error: Schema parsing
error: Error parsing attribute "@relation": The relation field `user` on Model `Referral` is required. This is no longer valid because it's not possible to enforce this constraint on the database level. Please change the field type from `User` to `User?` to fix this.
  -->  schema.prisma:21
   |
20 |   id   String @id @default(cuid()) @db.Char(30)
21 |   user User   @relation("UserToReferral")
22 | }
   |

Validation Error Count: 1
1reaction
janpiocommented, Nov 10, 2021

I can confirm that this schema causes a crash:

model User {
  id         String    @id @default(cuid()) @db.Char(30)
  referralId String    @unique @db.Char(30)
  referral   Referral? @relation("UserToReferral", fields: [referralId], references: [id])
}
model Referral {
  id   String @id @default(cuid()) @db.Char(30)
  user User   @relation("UserToReferral")
}

So although something might be weird or wrong with your schema, the crash is of course unexpected and should be a helpful validation error message instead.

λ npx prisma validate
Environment variables loaded from .env
Prisma schema loaded from prisma\schema.prisma
thread '<unnamed>' panicked at 'called `Option::unwrap()` on a `None` value', libs\datamodel\connectors\dml\src\model.rs:290:47
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: Unexpected token c in JSON at position 0

Using prisma format on the schema leads to validation errors:

λ npx prisma format
Environment variables loaded from .env
Prisma schema loaded from prisma\schema.prisma
Error: Schema parsing
error: Error parsing attribute "@relation": The relation fields `user` on Model `Referral` and `referral` on Model `User` both provide the `references` argument in the @relation attribute. You have to provide it only on one of the two fields.
  -->  schema.prisma:21
   |
20 |   id     String @id @default(cuid()) @db.Char(30)
21 |   user   User   @relation("UserToReferral", fields: [userId], references: [id])
22 |   userId String @db.Char(30)
   |
error: Error parsing attribute "@relation": The relation fields `user` on Model `Referral` and `referral` on Model `User` both provide the `references` argument in the @relation attribute. You have to provide it only on one of the two fields.
  -->  schema.prisma:16
   |
15 |   referralId String    @unique @db.Char(30)
16 |   referral   Referral? @relation("UserToReferral", fields: [referralId], references: [id], map: "User_referralId_fkey")
17 | }
   |
error: Error parsing attribute "@relation": The relation fields `user` on Model `Referral` and `referral` on Model `User` both provide the `fields` argument in the @relation attribute. You have to provide it only on one of the two fields.
  -->  schema.prisma:21
   |
20 |   id     String @id @default(cuid()) @db.Char(30)
21 |   user   User   @relation("UserToReferral", fields: [userId], references: [id])
22 |   userId String @db.Char(30)
   |
error: Error parsing attribute "@relation": The relation fields `user` on Model `Referral` and `referral` on Model `User` both provide the `fields` argument in the @relation attribute. You have to provide it only on one of the two fields.
  -->  schema.prisma:16
   |
15 |   referralId String    @unique @db.Char(30)
16 |   referral   Referral? @relation("UserToReferral", fields: [referralId], references: [id], map: "User_referralId_fkey")
17 | }
   |

Validation Error Count: 4

Using Format Document in the VSCode extension has a different effect:

model User {
  id         String    @id @default(cuid()) @db.Char(30)
  referralId String    @unique @db.Char(30)
  referral   Referral? @relation("UserToReferral", fields: [referralId], references: [id], map: "User_referralId_fkey")
}

model Referral {
  id     String @id @default(cuid()) @db.Char(30)
  user   User   @relation("UserToReferral", fields: [userId], references: [id])
  userId String @db.Char(30)
}

Running that again, keeps changing the schema!

model User {
  id         String    @id @default(cuid()) @db.Char(30)
  referralId String    @unique @db.Char(30)
  referral   Referral? @relation("UserToReferral", fields: [referralId], references: [id], map: "User_referralId_fkey")
}

model Referral {
  id     String @id @default(cuid()) @db.Char(30)
  user   User   @relation("UserToReferral", fields: [userId], references: [id])
  userId String @db.Char(30)@unique
}

(adds @unique to the last field without a space)

model User {
  id         String    @id @default(cuid()) @db.Char(30)
  referralId String    @unique @db.Char(30)
  referral   Referral? @relation("UserToReferral", fields: [referralId], references: [id], map: "User_referralId_fkey")
}

model Referral {
  id     String @id @default(cuid()) @db.Char(30)
  user   User   @relation("UserToReferral", fields: [userId], references: [id])
  userId String @unique @db.Char(30)
}

(fixes the order in the last field and adds the space)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Panicked at 'called `Option::unwrap()` on a `None` value ...
Hi! I'm currently porting the Lox language from Crafting Interpreters , but I've been stuck for a while in an annoying bug.
Read more >
Indexes - Prisma
Prisma allows configuration of database indexes, unique constraints and primary key constraints. This is in General Availability in versions 4.0.0 and later ...
Read more >
Thread 'main' panicked at 'called `Result::unwrap()` on an `Err ...
thread 'main' panicked at 'called Result::unwrap() on an Err value: Error("expected value", line: 1, column: 1)', src/models.rs:43:38
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found