postgresqlprismaprisma2bun

Could not figure out an ID in prisma.user.create() | Invalid Invocation | Prisma + Bunjs


I'm using BunJS and Prisma for my personal project and testing via cucumberjs and keploy. The following are my version:-

Name Version
Node v21.6.0
OS linux-arm64-openssl-3.0.x
Prisma Client 5.12.1
Query Engine 473ed3124229e22d881cb7addf559799debae1ab
Database postgresql

this is my prisma schema:-

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init

generator client {
  provider = "prisma-client-js"
}

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

model User {
  id        Int      @id @default(autoincrement())
  email     String   @unique
  name      String?
  password  String
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
 
  posts  Post[]
  
  comments  Comment[]
...
}

When i am running the application without any testing part integrated, it is working fine. But while testing, only in Post calls, i'm getting error

Invalid `prisma.user.create()` invocation:Could not figure out an ID in create. This is a non-recoverable error which probably happens when the Prisma Query Engine has a panic.

Here's my repo:- https://github.com/darkin424/Blog-Website .

To reproduce please follow the steps below:-

1. start the database docker instance
2. install `keploy` with :- https://keploy.io/docs/server/installation/
3. keploy record -c "bun --watch index.ts"
4. make few post api call for creation like `signup`, `create-post`
5. stop the docker instance
6. keploy test -c "bun --watch index.ts"

With this you can replicate the issue

I tried looking at similar problems other faced and tried running:-

bunx prisma format
bunx prisma validate
bunx prisma migrate dev
bunx prisma generate

if i consider post call's as noise my test works fine, but i want to test those POST calls as well.

Edit

So i updated the version of prisma to latest and now able to see the error better. In test mode, i'm getting the backtrace between my logs :-

thread 'tokio-runtime-worker' panicked at query-engine/connectors/sql-query-connector/src/database/operations/write.rs:194:22:
Could not figure out an ID in create
stack backtrace:
   0:     0xffff62e79450 - <unknown>
   1:     0xffff627db750 - <unknown>
   2:     0xffff62e5afb4 - <unknown>
   3:     0xffff62e7cfc4 - <unknown>
   4:     0xffff62e7c968 - <unknown>

Solution

  • I tried the above steps to replicate. The issue was not related with Prisma but how keploy is handling the creation of mocks for postgres, basically in the keploy the preparedQueryStatement was initialized with S since in most of database the sequence is "S1, S2, S3,..." but in case with prisma the query were created with "s_1, s_2, s_3,...", due to which the mocks weren't matched correctly with test resulting in failure. This was resolved in alpha-17 release of keploy and with which i got below result:

    🐰 Keploy: 2024-07-01T13:28:42+05:30    INFO    result  {"testcase id": "test-1", "testset id": "test-set-0", "passed": "true"}
    🐰 Keploy: 2024-07-01T13:28:42+05:30    INFO    starting test for of    {"test case": "test-2", "test set": "test-set-0"}
    Testrun failed for testcase with id: "test-2"
    
    --------------------------------------------------------------------
    
    +-------------------------------------------------------------------------------------------------------------+
    |                                                DIFFS TEST-2                                                 |
    +-------------------------------------------------------------------------------------------------------------+
    |                     EXPECT HEADER                    |                   ACTUAL HEADER                      |
    | -----------------------------------------------------+----------------------------------------------------- |
    |                                                      |                                                      |
    |                                                                                                             |
    |                      EXPECT BODY                     |                    ACTUAL BODY                       |
    | -----------------------------------------------------+----------------------------------------------------- |
    |    {                                                 |  {                                                   |
    |      "message": "User logged in successfully",       |    "message": "User logged in successfully",         |
    |   -  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9. | +  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.   |
    |   eyJpZCI6MSwiZW1haWwiOiJpc3NzYWxjdXBuYW1kZUBhYmMuY2 | eyJpZCI6MSwiZW1haWwiOiJpc3NzYWxjdXBuYW1kZUBhYmMuY2   |
    |   9tIiwiaWF0IjoxNzE5ODIwNDE2LCJleHAiOjE3MTk5MDY4MTZ9 | 9tIiwiaWF0IjoxNzE5ODIwNzIyLCJleHAiOjE3MTk5MDcxMjJ9   |
    |   .vPfugYdsBOlI-0LCdmLKADEEjRXz6FViBRWTJCKQTq8"      | ._q0QLMJ-6_Y588JLq3fzc3qtBwYEKzdnAQe9z-4PeeQ"        |
    |    }                                                 |  }                                                   |
    |                                                      |                                                      |
    |                                                                                                             |
    +-------------------------------------------------------------------------------------------------------------+
    🐰 Keploy: 2024-07-01T13:28:42+05:30    INFO    result  {"testcase id": "test-2", "testset id": "test-set-0", "passed": "false"}
    🐰 Keploy: 2024-07-01T13:28:42+05:30    INFO    starting test for of    {"test case": "test-3", "test set": "test-set-0"}
    Testrun passed for testcase with id: "test-3"
    
    --------------------------------------------------------------------
    
    🐰 Keploy: 2024-07-01T13:28:42+05:30    INFO    result  {"testcase id": "test-3", "testset id": "test-set-0", "passed": "true"}
    🐰 Keploy: 2024-07-01T13:28:42+05:30    INFO    starting test for of    {"test case": "test-4", "test set": "test-set-0"}
    Testrun passed for testcase with id: "test-4"
    
    --------------------------------------------------------------------
    
    🐰 Keploy: 2024-07-01T13:28:42+05:30    INFO    result  {"testcase id": "test-4", "testset id": "test-set-0", "passed": "true"}
    
     <=========================================> 
      TESTRUN SUMMARY. For test-set: "test-set-0"
            Total tests: 4
            Total test passed: 3
            Total test failed: 1
     <=========================================> 
    

    The only one which failed was due to token expiration, which i passed by using --freeze-time flag.