Skip to content

Create fail with multiple connectOrCreate and connect #4230

@sheandemontigny

Description

@sheandemontigny

Bug description

When trying to use 2 connectOrCreate with 2 connect during a create(), prisma throw the following error:

Invalid prisma.record.create() invocation:
Error occurred during query execution:
EnvVarNotFound("Expected parent binding '1' to be present.")
at PrismaClientFetcher.request (..\node_modules@prisma\client\runtime\index.js:78786:15)

Here is the create function:

const config = await prisma.recordConfig.create({ data: {} });
const container = await prisma.container.create({ data: {} });
await prisma.record.create({
  data: {
    container: { connect: { id: container.id } },
    config: { connect: { id: config.id } },
    location: {
      connectOrCreate: {
        create: {
          location: "some_location",
        },
        where: {
          location: "some_location",
        },
      },
    },
    type: {
      connectOrCreate: {
        create: {
          type: "a",
        },
        where: {
          type: "a",
        },
      },
    },
  },
});

If I remove one of relation with the connectOrCreate or one of the connect, the record is created without error.

How to reproduce

  1. Create 4 tables with a primary key
  2. Create a table with a foreign key for each other table (4 in our case)
  3. Create a call to connect(...) where you connectOrCreate 2 of the 4 table and connect the other 2 table

Expected behavior

  1. Should create the missing record with the connectOrCreate and connect with connect

Prisma information

Prisma Schema:

model Container {
  id     Int      @id @default(autoincrement())

  Record Record[]
}

model RecordConfig {
  id     Int      @id @default(autoincrement())

  Record Record[]
}

model RecordLocation {
  id       Int    @id @default(autoincrement())
  location String @unique

  Record Record[]
}

model RecordType {
  id     Int      @id @default(autoincrement())
  type   String   @unique

  Record Record[]
}

model Record {
  id           Int            @id @default(autoincrement())
  location     RecordLocation @relation(fields: [locationId], references: [id])
  locationId   Int
  type         RecordType     @relation(fields: [recordTypeId], references: [id])
  recordTypeId Int
  config       RecordConfig?  @relation(fields: [configId], references: [id])
  configId     Int?
  container    Container      @relation(fields: [containerId], references: [id])
  containerId  Int
}

Prisma Client Query:

prisma:info  Starting a postgresql pool with 13 connections.
prisma:info  Started http server
prisma:query BEGIN
prisma:query INSERT INTO "public"."RecordConfig" DEFAULT VALUES RETURNING "public"."RecordConfig"."id"
prisma:query SELECT "public"."RecordConfig"."id" FROM "public"."RecordConfig" WHERE "public"."RecordConfig"."id" = $1 LIMIT $2 OFFSET $3
prisma:query COMMIT
prisma:query BEGIN
prisma:query INSERT INTO "public"."Container" DEFAULT VALUES RETURNING "public"."Container"."id"
prisma:query SELECT "public"."Container"."id" FROM "public"."Container" WHERE "public"."Container"."id" = $1 LIMIT $2 OFFSET $3
prisma:query COMMIT
prisma:query BEGIN
prisma:query SELECT "public"."RecordType"."id" FROM "public"."RecordType" WHERE "public"."RecordType"."type" = $1 OFFSET $2
prisma:query INSERT INTO "public"."RecordType" ("type") VALUES ($1) RETURNING "public"."RecordType"."id"
prisma:query SELECT "public"."RecordLocation"."id" FROM "public"."RecordLocation" WHERE "public"."RecordLocation"."location" = $1 OFFSET $2
prisma:query SELECT "public"."RecordConfig"."id" FROM "public"."RecordConfig" WHERE "public"."RecordConfig"."id" = $1 OFFSET $2
prisma:query SELECT "public"."Container"."id" FROM "public"."Container" WHERE "public"."Container"."id" = $1 OFFSET $2
prisma:query INSERT INTO "public"."RecordLocation" ("location") VALUES ($1) RETURNING "public"."RecordLocation"."id"
prisma:query ROLLBACK

Environment & setup

- OS: Windows
- Database: PostgreSQL
- Node.js version: 12.18.4
- Prisma version: 2.11.0

Reproduction repository : https://github.com/sheandemontigny/prisma-EnvVarNotFound-error

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions