Prisma-client-js: Failed to create entry, "Expected a valid parent ID to be present for a nested connect on a one-to-many relation."

Created on 24 Mar 2020  ·  8Comments  ·  Source: prisma/prisma-client-js

Bug description


Upon trying to create a specific entry for a model, the following error occurs:

↵Invalid `prisma.userAction.create()` invocation in↵C:\Users\sacha\api\src\LogAction.ts:8:44↵↵   4   context: Context,
↵   5   action: string,
↵   6   userId: string,
↵   7   credentialId?: string
↵→  8 ) ↵↵Error occurred during query execution:↵InterpretationError("Error for binding \'1\': AssertionError(\"[Query Graph] Expected a valid parent ID to be present for a nested connect on a one-to-many relation.\")")
prisma-client Prisma Client call: +37ms
  prisma-client prisma.userAction.create({
  prisma-client   data: {
  prisma-client     action: 'Login',
  prisma-client     ip: '::1',
  prisma-client     associatedToken: {
  prisma-client       connect: {
  prisma-client         id: 'oR60lHj-frfVik8JqpsiuA'
  prisma-client       }
  prisma-client     },
  prisma-client     user: {
  prisma-client       connect: {
  prisma-client         id: 'ck8545h5o00017kvspz8hflik'
  prisma-client       }
  prisma-client     }
  prisma-client   }
  prisma-client }) +14ms
  prisma-client Generated request: +2ms
  prisma-client mutation {
  prisma-client   createOneUserAction(data: {
  prisma-client     action: "Login"
  prisma-client     ip: "::1"
  prisma-client     associatedToken: {
  prisma-client       connect: {
  prisma-client         id: "oR60lHj-frfVik8JqpsiuA"
  prisma-client       }
  prisma-client     }
  prisma-client     user: {
  prisma-client       connect: {
  prisma-client         id: "ck8545h5o00017kvspz8hflik"
  prisma-client       }
  prisma-client     }
  prisma-client   }) {
  prisma-client     id
  prisma-client     action
  prisma-client     ip
  prisma-client     createdAt
  prisma-client     updatedAt
  prisma-client   }
  prisma-client }
  prisma-client  +12ms
  printStack callsite Error
    at Object.s [as UserAction] (C:\Users\sacha\api\node_modules\@prisma\client\runtime\index.js:1:44641)
    at Object.n.<computed> [as create] (C:\Users\sacha\api\node_modules\@prisma\client\runtime\index.js:1:46267)
    at Object.logAction (C:\Users\sacha\api\src\LogAction.ts:8:44)
    at authenticate (C:\Users\sacha\api\src\graphql\resolvers\mutation\Auth\authenticate.ts:51:31)
    at processTicksAndRejections (internal/process/task_queues.js:97:5) +0ms



md5-1bb468941c599e340b73ce6415625c9a



model User {
  id                    String   @default(cuid()) @id
  activated             Boolean  @default(false)
  activationClaim       String   @default(cuid()) @unique
  username              String   @unique
  name                  String
  avatar                String   @default("data:image/png;base64,iVB...")
  password              String?
  createdAt             DateTime @default(now())
  updatedAt             DateTime @updatedAt
  groups                Group[]
  tokens                Token[]
  actions               UserAction[]
  linkedAccounts        LinkedAccount[]
  credentials           UserCredential[]
}

model UserAction {
  id                    String   @default(cuid()) @id
  action                String
  ip                    String
  createdAt             DateTime @default(now())
  updatedAt             DateTime @updatedAt
  associatedToken       Token
  user                  User
}

model Token {
  id                    String   @id
  type                  TokenUse
  revoked               Boolean  @default(false)
  lastIp                String?
  lastUserAgent         String?
  createdAt             DateTime @default(now())
  updatedAt             DateTime @updatedAt
  associatedTokens      Token[]
  UserActions           UserAction[]
  userCredential        UserCredential?
  user                  User?
}



md5-ea6c706cb01aa18d29c21c9e107729cb



  await context.prisma.userAction.create({
    data: {
      action,
      ip: context.req.ip,
      associatedToken: {
        connect: {
          id: credentialId ? credentialId : context.tokenId
        }
      },
      user: {
        connect: {
          id: userId
        }
      }
    }
  });

Expected behavior


Model is created.

Environment & setup


Windows 10
postgres 12.1
[email protected]
node v13.8.0

Most helpful comment

Fixed, Turns out a bug in my code was passing an id that was not present in my database, may be worth having a clearer message.

All 8 comments

Fixed, Turns out a bug in my code was passing an id that was not present in my database, may be worth having a clearer message.

Absolutely! Can you maybe help us out @sachaw by creating an easy reproduction of what you were accidentally doing? Maybe create a new issue with a title that mentions that the error message is not super clear and should be improved. If you can include a schema and a Prisma Client call (as you did above!) that triggers that, it will be super easy to reproduce for us and then reason about the change. Thanks!

I just came across this, too. I can't share any of the codebase but I can tell you that I was trying to create an item in my database and in the custom resolver, I was first fetching the details of another item by its id. I'd forgotten, however, to seed my database with this updated item, hence the item with that id didn't exist and this findOne was failing.

Similar issue to @darrylyoung, it would be good to log some info on the relation and the id used, whether it was a create or update attempt etc.
The logged line number also currently points to the top level create/update definition, it would be good to instead point to the nested attempt

Thanks this solved my problem, i was providing a wrong id to update

That is a good point @jamestowers. Can someone post a reproduction in some way so we can turn this into a new issue with an example of the "bad" error message and what it should/could look like? @Arturo-Lopez maybe, as you just recently hit the same problem?

@janpio I have put together a simple reproduction here: https://github.com/Pat-Ayres/prisma-error-message-reproduction

Error:                                                                                                                Invalid `prisma.comment.create()` invocation in
/home/payres/tmpsrc/prisma-error-message-reproduction/prisma/seed.ts:9:26

   5
   6 const prisma = new PrismaClient(prismaConfig);
   7
   8 async function main(): Promise<void> {
→  9     await prisma.comment.create(
  Query interpretation error. Error for binding '2': AssertionError("[Query Graph] Expected a valid parent ID to be present for a nested connect on a one-to-many relation.")
    at iy.request (/home/payres/tmpsrc/prisma-error-message-reproduction/node_modules/@prisma/client/runtime/src/runtime/getPrismaClient.ts:1181:15)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

This example has a create with 2 connects where the IDs for the connected objects do not exist. It would be helpful if we were given more information, e.g. which connect(s) it failed on, and potentially the ID that does not exist.

something like "Expected a valid parent ID to be present for a nested connect on a one-to-many relation [Connect: user, ID: 9000], [Connect: Post, ID: 9000]"

Hopefully this is helpful!

That is awesome @Pat-Ayres, I turned it into a new issue at https://github.com/prisma/prisma-client-js/issues/914 which you can follow.

Was this page helpful?
0 / 5 - 0 ratings