connectOrCreate fails during upsert
See original GitHub issueBug 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
- Create 2 tables - the primary key on each being a string (varchar in our case)
- Create an intermediate table using a composite primary key from the two tables created in 1.
- Add a foreign key on the intermediate table connecting primaryKey1 to table1 and primaryKey2 to table2
- On table1 create a record.
- Create a call to
upsert(..)
where you connect to the record on table1 and thenconnectOrCreate
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:
Expected behavior
- Prisma should realize that the intermediate relationship doesn’t exist during
upsert
✔️ - Prisma should first check table1 for an appropriate record.
- Prisma then checks table2 for a second record.
- Upon realizing that the record doesn’t exist on table2 Prisma should create the record.
- 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:
- Created 3 years ago
- Reactions:2
- Comments:5 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
A fix has been merged, this will be available in the 2.10 release.
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.