prisma / prisma/orm

nested connectOrCreate when upserting into Many-to-many relations does not have a proper "where" clause

Open
#15,134 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug/1-unconfirmed kind/bug tech/typescript topic: client types
Dominant language
TypeScript
Stars
47.6k
Forks
2.5k
Avg merge
21h 59m
Merged PRs (30d)
95

Description

Bug description

I can't tell it connect or create a new tag that has this unique name:

prisma.post.upsert({
  where: { id },
  update: { title, text },
  create: {
    id,
    title,
    text,
    tags: {
      connectOrCreate: tags.map((tagName) => ({
        where: { name: tagName }, // ⬅️
        create: { name: tagName, description: tagName },
      })),
    },
  },
})
TS Error
Type '{ where: { name: string; }; create: { name: string; description: string; }; }[]' is not assignable to type 'Enumerable<PostsOnTagsCreateOrConnectWithoutPostInput> | undefined'.
  Type '{ where: { name: string; }; create: { name: string; description: string; }; }[]' is not assignable to type 'PostsOnTagsCreateOrConnectWithoutPostInput[]'.
    Type '{ where: { name: string; }; create: { name: string; description: string; }; }' is not assignable to type 'PostsOnTagsCreateOrConnectWithoutPostInput'.
      Types of property 'where' are incompatible.
        Type '{ name: string; }' has no properties in common with type 'PostsOnTagsWhereUniqueInput'.ts(2322)
How to reproduce

https://github.com/mohsen1/prisma-connectOrCreate-where

Expected behavior

The where clause should allow me to specify which tags I want to connect or create

Prisma information
schema.prisma
model Tag {
    id    Int           @id @default(autoincrement())
    name  String        @unique
    posts PostsOnTags[]
}

model PostsOnTags {
    postId     Int
    post       Post     @relation(fields: [postId], references: [id])
    tagId      Int
    tag        Tag      @relation(fields: [tagId], references: [id])
    assignedAt DateTime @default(now())

    @@id([postId, tagId])
}

model Post {
    id        Int           @id @default(autoincrement())
    createdAt DateTime      @default(now())
    updatedAt DateTime      @updatedAt
    title     String        @db.VarChar(255)
    content   String        @db.Text()
    tags      PostsOnTags[]
}
Environment & setup
  • OS: macOS
  • Database: MySQL 8
  • Node.js version: 16
Prisma Version
   "@prisma/client": "^4.3.0",
    "@types/node": "^18.7.14",
    "prisma": "^4.3.0",
    "typescript": "^4.8.2"

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked prisma-connectOrCreate-where reproduction and its schema.prisma, focusing on the nested many-to-many connectOrCreate input and the reported TypeScript error. Run the reproduction to confirm that the unique Tag.name field is rejected in where; done means the where clause accepts the tag's unique name while preserving the expected connect-or-create behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, typescript
Domain
backend-api-design, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.