Skip to content

Create a single record and multiple related records with optional relation to another entity returns inconsistent order #14447

Open

Description

@yss14

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.

https://github.com/yss14/prisma-reproducible-example-nested-writes-optional-relation

Create a .env file and provide a DATABASE_URL environment variable

npm 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

  1. added
    kind/bug
    on Jul 22, 2022
  2. added
    bug/1-unconfirmed
    on Jul 22, 2022
  3. added
    topic: nested write (create/update/delete)
    on Jul 30, 2022
  4. added
    tech/engines
    on Aug 4, 2022
  5. Jolg42 commented on Sep 8, 2022

    @Jolg42
    Contributor

    Related #14351

  6. self-assigned this
    on Sep 13, 2022
  7. pimeys commented on Sep 13, 2022

    @pimeys

    Yep, it's still happening with 4.3.1.

    Here's additionally the debug logs for the dev who picks this up:

      prisma:client  Prisma Client call: +5ms
      prisma:client  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
      }
    }) +1ms
      prisma:client  Generated request: +0ms
      prisma:client  mutation {
      createOneGroup(data: {
        name: "Group without capacity"
        slots: {
          create: [
            {
              from: "2022-07-22T00:00:00.000Z"
            },
            {
              from: "2022-07-23T00:00:00.000Z"
            },
            {
              from: "2022-07-24T00:00:00.000Z"
            }
          ]
        }
      }) {
        id
        name
        slots {
          id
          from
          groupId
          capacityId
        }
      }
    }
     +0ms
      prisma:client:libraryEngine  sending request, this.libraryStarted: true +6ms
    prisma:query BEGIN
    prisma:query INSERT INTO "public"."Group" ("name") VALUES ($1) RETURNING "public"."Group"."id" /* traceparent=00-00-00-00 */
    prisma:query INSERT INTO "public"."Slot" ("from","groupId") VALUES ($1,$2) RETURNING "public"."Slot"."id" /* traceparent=00-00-00-00 */
    prisma:query INSERT INTO "public"."Slot" ("from","groupId") VALUES ($1,$2) RETURNING "public"."Slot"."id" /* traceparent=00-00-00-00 */
    prisma:query INSERT INTO "public"."Slot" ("from","groupId") VALUES ($1,$2) RETURNING "public"."Slot"."id" /* traceparent=00-00-00-00 */
    prisma:query SELECT "public"."Group"."id", "public"."Group"."name" FROM "public"."Group" WHERE "public"."Group"."id" = $1 LIMIT $2 OFFSET $3 /* traceparent=00-00-00-00 */
    prisma:query SELECT "public"."Slot"."id", "public"."Slot"."from", "public"."Slot"."groupId", "public"."Slot"."capacityId" FROM "public"."Slot" WHERE "public"."Slot"."groupId" IN ($1) OFFSET $2
    prisma:query COMMIT
      prisma:client  Prisma Client call: +5ms
      prisma:client  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
      }
    }) +1ms
      prisma:client  Generated request: +0ms
      prisma:client  mutation {
      createOneGroup(data: {
        name: "Group with capacity"
        slots: {
          create: [
            {
              from: "2022-07-22T00:00:00.000Z"
              capacity: {
                create: {
                  count: 10
                }
              }
            },
            {
              from: "2022-07-23T00:00:00.000Z"
              capacity: {
                create: {
                  count: 10
                }
              }
            },
            {
              from: "2022-07-24T00:00:00.000Z"
              capacity: {
                create: {
                  count: 10
                }
              }
            }
          ]
        }
      }) {
        id
        name
        slots {
          id
          from
          groupId
          capacityId
        }
      }
    }
     +0ms
      prisma:client:libraryEngine  sending request, this.libraryStarted: true +6ms
    prisma:query BEGIN
    prisma:query INSERT INTO "public"."Group" ("name") VALUES ($1) RETURNING "public"."Group"."id" /* traceparent=00-00-00-00 */
    prisma:query INSERT INTO "public"."Capacity" ("count") VALUES ($1) RETURNING "public"."Capacity"."id" /* traceparent=00-00-00-00 */
    prisma:query INSERT INTO "public"."Slot" ("from","groupId","capacityId") VALUES ($1,$2,$3) RETURNING "public"."Slot"."id" /* traceparent=00-00-00-00 */
    prisma:query INSERT INTO "public"."Capacity" ("count") VALUES ($1) RETURNING "public"."Capacity"."id" /* traceparent=00-00-00-00 */
    prisma:query INSERT INTO "public"."Slot" ("from","groupId","capacityId") VALUES ($1,$2,$3) RETURNING "public"."Slot"."id" /* traceparent=00-00-00-00 */
    prisma:query INSERT INTO "public"."Capacity" ("count") VALUES ($1) RETURNING "public"."Capacity"."id" /* traceparent=00-00-00-00 */
    prisma:query INSERT INTO "public"."Slot" ("from","groupId","capacityId") VALUES ($1,$2,$3) RETURNING "public"."Slot"."id" /* traceparent=00-00-00-00 */
    prisma:query SELECT "public"."Group"."id", "public"."Group"."name" FROM "public"."Group" WHERE "public"."Group"."id" = $1 LIMIT $2 OFFSET $3 /* traceparent=00-00-00-00 */
    prisma:query SELECT "public"."Slot"."id", "public"."Slot"."from", "public"."Slot"."groupId", "public"."Slot"."capacityId" FROM "public"."Slot" WHERE "public"."Slot"."groupId" IN ($1) OFFSET $2
    prisma:query COMMIT
      prisma:client:libraryEngine  library stopping +10ms
      prisma:client:libraryEngine  library stopped +0ms
      prisma:client:libraryEngine:exitHooks  exit event received: beforeExit +0ms
      prisma:client:libraryEngine:exitHooks  exit event received: exit +0ms
    
  8. assigned and unassigned on Sep 13, 2022
  9. added this to the 4.4.0 milestone on Sep 14, 2022
  10. pimeys commented on Sep 19, 2022

    @pimeys

    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

  11. dan2kx commented on Nov 14, 2024

    @dan2kx

    Any progress with this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug/2-confirmedkind/bugtech/enginestopic: nested write (create/update/delete)topic: postgresql

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions