-
Notifications
You must be signed in to change notification settings - Fork 2.1k
MongoDB: upsert() fails on update with relation when notablescan: 1 #12793
Copy link
Copy link
Closed
Labels
bug/2-confirmedBug has been reproduced and confirmed.Bug has been reproduced and confirmed.kind/bugA reported bug.A reported bug.topic: indexestopic: mongodbtopic: performancetopic: performance/queriestopic: updateNested query `update`Nested query `update`topic: upsert()
Milestone
Description
Bug description
Using MongoDB, the following error occurs when running an upsert and the document already exists:
PrismaClientUnknownRequestError:
Invalid `prisma.user.upsert()` invocation:
Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: RawDatabaseError { code: "unknown", message: "Command failed (NoQueryExecutionPlans): error processing query: ns=ninja-diary.projectsTree: $expr {$and: [{$in: [\"$ownerId\", {$const: [\"qGdWa0VNzHfW8GRaDxz5nfjPDRt1\"]}]}, {$or: [{$ne: [{$ifNull: [\"$ownerId\", {$const: null}]}, {$const: null}]}, {$eq: [\"$ownerId\", {$const: null}]}]}]}Sort: {}\nProj: { _id: true, ownerId: true }\nCollation: { locale: \"simple\" }\n planner returned error :: caused by :: No indexed plans available, and running with 'notablescan')" } })
at Object.request (/usr/app/node_modules/@prisma/client/runtime/index.js:45582:15)
at async PrismaClient._request (/usr/app/node_modules/@prisma/client/runtime/index.js:46405:18)
at async setSyncTimeForUser (/usr/app/src/features/sync/utils.ts:29:18)
at async /usr/app/src/features/sync/push.ts:19:5
at async Proxy._transactionWithCallback (/usr/app/node_modules/@prisma/client/runtime/index.js:46366:18)
at async push (/usr/app/src/features/sync/push.ts:15:3)
at async /usr/app/src/app.ts:442:7 {
clientVersion: '3.12.0'
}
How to reproduce
model User {
id String @id @map("_id") @db.String
name String
lastSync DateTime @updatedAt
projects Project[]
@@map("users")
}
model Project {
id String @id @map("_id") @db.String
name String
owner User @relation(fields: [ownerId], references: [id])
ownerId String @db.String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
isDeleted Boolean @default(false)
@@index([ownerId])
@@index([createdAt])
@@index([updatedAt])
@@index([isDeleted])
@@map("projects")
}Run this once:
await prisma.user.upsert({
where: { id: userId },
create: { id: userId, name: "foo" },
update: { name: "foo2" },
});This will create the document, which works.
Then run it a second time, which will execute the "update" part since the document exists. It now fails with the error above.
If I remove the relation from the schema (and only have a "dum" ownerId field) the opertation works again.
Expected behavior
It should not fail
Prisma information
Prisma v3.12.0
mongoDB 4.4
model User {
id String @id @map("_id") @db.String
name String
lastSync DateTime @updatedAt
projects Project[]
@@map("users")
}
model Project {
id String @id @map("_id") @db.String
name String
owner User @relation(fields: [ownerId], references: [id])
ownerId String @db.String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
isDeleted Boolean @default(false)
@@index([ownerId])
@@index([createdAt])
@@index([updatedAt])
@@index([isDeleted])
@@map("projects")
}Run this once:
await prisma.user.upsert({
where: { id: userId },
create: { id: userId, name: "foo" },
update: { name: "foo2" },
});Environment & setup
- OS: debian
- Database: MongoDB Atlas v4.4
- Node.js version: 16.13
Prisma Version
prisma : 3.12.0
@prisma/client : 3.12.0
Current platform : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine 22b822189f46ef0dc5c5b503368d1bee01213980 (at node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine : migration-engine-cli 22b822189f46ef0dc5c5b503368d1bee01213980 (at node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine : introspection-core 22b822189f46ef0dc5c5b503368d1bee01213980 (at node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary : prisma-fmt 22b822189f46ef0dc5c5b503368d1bee01213980 (at node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash : 22b822189f46ef0dc5c5b503368d1bee01213980
Studio : 0.459.0
Preview Features : interactiveTransactions, extendedIndexes
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bug/2-confirmedBug has been reproduced and confirmed.Bug has been reproduced and confirmed.kind/bugA reported bug.A reported bug.topic: indexestopic: mongodbtopic: performancetopic: performance/queriestopic: updateNested query `update`Nested query `update`topic: upsert()