Repository navigation
Description
Bug description
With the provided prisma schema the creation order of the related records depends on whether I link an additional related entity or not when performing a "create single record and multiple related records" operation with prisma.
With no optional related entity provided, the creation order reflects the input order passed to the nested create, with an optional related entity provided the creation order somehow is reversed.
How to reproduce
Clone the reproducible github repo and follow the steps in the readme.
Create a
.envfile and provide aDATABASE_URLenvironment variablenpm i npm run prisma:generate npm run prisma:migrate-deploy npm start
Expected behavior
The creation order should in both cases be the same. Otherwise it is hard to relate on the creation order, e.g. when writing integration test cases or matching results to the corresponding input data.
Prisma information
Schema
model Group {
id Int @id @default(autoincrement())
name String
slots Slot[]
}
model Slot {
id Int @id @default(autoincrement())
from DateTime
groupId Int
group Group @relation(fields: [groupId], references: [id])
capacityId Int?
capacity Capacity? @relation(fields: [capacityId], references: [id])
}
model Capacity {
id Int @id @default(autoincrement())
count Int
slots Slot[]
}
Code
const newGroupWithoutCapacity = await prisma.group.create({
data: {
name: "Group without capacity",
slots: {
create: [
{ from: new Date("2022-07-22T00:00:00.000Z") },
{ from: new Date("2022-07-23T00:00:00.000Z") },
{ from: new Date("2022-07-24T00:00:00.000Z") },
],
},
},
include: {
slots: true,
},
});
console.log(JSON.stringify({ newGroupWithoutCapacity }, null, 2));
/*
Creation order matches the input order
{
"newGroupWithoutCapacity": {
"id": 9,
"name": "Group without capacity",
"slots": [
{
"id": 25,
"from": "2022-07-22T00:00:00.000Z",
"groupId": 9,
"capacityId": null
},
{
"id": 26,
"from": "2022-07-23T00:00:00.000Z",
"groupId": 9,
"capacityId": null
},
{
"id": 27,
"from": "2022-07-24T00:00:00.000Z",
"groupId": 9,
"capacityId": null
}
]
}
}
*/
const newGroupWithCapacity = await prisma.group.create({
data: {
name: "Group with capacity",
slots: {
create: [
{
from: new Date("2022-07-22T00:00:00.000Z"),
capacity: { create: { count: 10 } },
},
{
from: new Date("2022-07-23T00:00:00.000Z"),
capacity: { create: { count: 10 } },
},
{
from: new Date("2022-07-24T00:00:00.000Z"),
capacity: { create: { count: 10 } },
},
],
},
},
include: {
slots: true,
},
});
console.log(JSON.stringify({ newGroupWithCapacity }, null, 2));
/*
Creation order is reversed input order
{
"newGroupWithCapacity": {
"id": 10,
"name": "Group with capacity",
"slots": [
{
"id": 28,
"from": "2022-07-24T00:00:00.000Z",
"groupId": 10,
"capacityId": 13
},
{
"id": 29,
"from": "2022-07-23T00:00:00.000Z",
"groupId": 10,
"capacityId": 14
},
{
"id": 30,
"from": "2022-07-22T00:00:00.000Z",
"groupId": 10,
"capacityId": 15
}
]
}
}
*/Environment & setup
- OS: macOS 11.5.2
- Database: PostgreSQL 14.2 on aarch64-apple-darwin20.6.0, compiled by Apple clang version 13.0.0 (clang-1300.0.29.30), 64-bit
- Node.js version: v18.0.0
Prisma Version
prisma : 4.1.0
@prisma/client : 4.1.0
Current platform : darwin-arm64
Query Engine (Node-API) : libquery-engine 8d8414deb360336e4698a65aa45a1fbaf1ce13d8 (at node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Migration Engine : migration-engine-cli 8d8414deb360336e4698a65aa45a1fbaf1ce13d8 (at node_modules/@prisma/engines/migration-engine-darwin-arm64)
Introspection Engine : introspection-core 8d8414deb360336e4698a65aa45a1fbaf1ce13d8 (at node_modules/@prisma/engines/introspection-engine-darwin-arm64)
Format Binary : prisma-fmt 8d8414deb360336e4698a65aa45a1fbaf1ce13d8 (at node_modules/@prisma/engines/prisma-fmt-darwin-arm64)
Default Engines Hash : 8d8414deb360336e4698a65aa45a1fbaf1ce13d8
Studio : 0.467.0
Preview Features : interactiveTransactions
Activity
Jolg42 commentedon Sep 8, 2022
Related #14351
pimeys commentedon Sep 13, 2022
Yep, it's still happening with 4.3.1.
Here's additionally the debug logs for the dev who picks this up:
pimeys commentedon Sep 19, 2022
This is quite complex issue to solve. The analysis is in an internal document, and it's about the graph modification parts where we swap parent and child queries, where we go wrong: https://www.notion.so/prismaio/Spec-Wrong-Related-Item-Insertion-Order-4306d2ae32ba4404a7b5130c2a66bf04
dan2kx commentedon Nov 14, 2024
Any progress with this?