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.

connectOrCreate fails during upsert

See original GitHub issue

Bug description

@kazimirzak and I have run into some issues while trying to upsert and connectOrCreate at the same time.

When trying to use connectOrCreate during an upsert(), prisma will throw the following error:

Error occurred during query execution: EnvVarNotFound(“Expected parent binding '4' to be present.”) at PrismaClientFetcher.request (…\node_modules@prisma\client\runtime\index.js:1:228459)

“4” is not part of any variable being passed to the upsert function.

The data we’re trying create looks as follows:

this.client.serverToSinner.upsert({
    where: {
        serverId_sinnerId: {
            serverId,
            sinnerId
        }
    },
    create: {
        Server: {
            connect: {
                id: serverId
            }
        },
        Sinner: {
            connectOrCreate: {
                where: {
                    id: sinnerId
                },
                create: {
                    id: sinnerId,
                    username: sinnerUsername,
                }
            }
        },
        absolvedAt
    },
    update: {
        absolvedAt
    }
}

serverToSinner is an intermediate table.

It’s worth noting that, if we change connectOrCreate to connect and make sure the record is already there, it works perfectly fine.

How to reproduce

  1. Create 2 tables - the primary key on each being a string (varchar in our case)
  2. Create an intermediate table using a composite primary key from the two tables created in 1.
  3. Add a foreign key on the intermediate table connecting primaryKey1 to table1 and primaryKey2 to table2
  4. On table1 create a record.
  5. Create a call to upsert(..) where you connect to the record on table1 and then connectOrCreate on table2, thus a new record should be created on table2 and then a record connecting record1 and record2 in the intermediate table.

The database should look somewhat like this: image

Expected behavior

  1. Prisma should realize that the intermediate relationship doesn’t exist during upsert ✔️
  2. Prisma should first check table1 for an appropriate record.
  3. Prisma then checks table2 for a second record.
  4. Upon realizing that the record doesn’t exist on table2 Prisma should create the record.
  5. The intermediate table should be updated, connecting the newly created record on table2 to the record on table1.

Prisma information

model Server {
  id             String           @id
  name           String?
  channelId      String?
  ServerToSinner ServerToSinner[] @relation("ServerTo_ServerToSinner")
}

model Sinner {
  id             String           @id
  username       String
  ServerToSinner ServerToSinner[] @relation("SinnerTo_ServerToSinner")
}

model ServerToSinner {
  serverId       String
  sinnerId       String
  questionsAsked Int      @default(0)
  absolvedAt     DateTime
  Server         Server   @relation("ServerTo_ServerToSinner", fields: [serverId], references: [id])
  Sinner         Sinner   @relation("SinnerTo_ServerToSinner", fields: [sinnerId], references: [id])

  @@id([serverId, sinnerId])
  @@index([sinnerId], name: "fk_sinner")
  @@map("_ServerToSinner")
}

Environment & setup

OS: Windows 10, Version 2004
Database: MySQL
Node.js version: 14.9
Primsa: 2.6.2

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
dpetrickcommented, Oct 23, 2020

A fix has been merged, this will be available in the 2.10 release.

2reactions
stefanoruthcommented, Sep 14, 2020

Can confirm and reproduce this issue, gets the same error Expected parent binding '4' to be present. when running the query.

One thing to note tho is that it works if you turn it around and and use connectOrCreate on Server and just connect on Sinner then it just works.

If you then use connectOrCreate on both queries then error changes binding number to Expected parent binding '8' to be present. where it looks like it run the Server part of the query find but then fails when it comes to Sinner, making it seems like there is some issue with the relation to Sinner.

Also trying moving the @id of the ServerToSinner table to its own column and making that its only index but its changes nothing, unfortunately.

Read more comments on GitHub >

github_iconTop Results From Across the Web

can i do a nested connectOrCreate from within an upsert quer
can i do a nested connectOrCreate from within an upsert query i m getting an error that prisma doesn t understand my args...
Read more >
can i use "AND" to where in connectOrCreate for prisma?
I want to find accountNum with userId and accountNumber so i use AND in where. but there are error. Unknown arg AND in...
Read more >
Relation queries (Concepts) - Prisma
Provide transactional guarantees for creating, updating or deleting data across multiple tables in a single Prisma Client query. If any part of the...
Read more >
3.0.0 Migration - Neo4j GraphQL Library
Simply update @neo4j/graphql using npm or your package manager of choice: ... the schema definition and the actual data may now fail in...
Read more >
Prisma multiple create queries with nested connectOrCreate ...
... with nested connectOrCreate throws unique constraint failed-postgresql. ... connectOrCreate queries that run as concurrent transactions can result in a ...
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