title: Prisma many-to-many relations: create and connect
body: <p>In my Prisma schema, I have a many-to-many relationship between posts and categories. I've added <code>@map</code> options to match the Postgres snake_case naming convention:</p>
  <pre><code>model Post {
    id         Int            @id @default(autoincrement())
    title      String
    body       String?
    categories PostCategory[]
  
    @@map(&quot;post&quot;)
  }
  
  model Category {
    id    Int            @id @default(autoincrement())
    name  String
    posts PostCategory[]
  
    @@map(&quot;category&quot;)
  }
  
  model PostCategory {
    categoryId Int      @map(&quot;category_id&quot;)
    postId     Int      @map(&quot;post_id&quot;)
    category   Category @relation(fields: [categoryId], references: [id])
    post       Post     @relation(fields: [postId], references: [id])
  
    @@id([categoryId, postId])
    @@map(&quot;post_category&quot;)
  }
  </code></pre>
  <p>I'm trying to create a post with multiple categories at the same time. If a category exists, I'd like to <code>connect</code> the category to the post. If the category doesn't exist, I'd like to create it. The creation part is working well, but the connection part is problematic:</p>
  <pre><code>  await prisma.post.create({
      data: {
        title: 'Hello',
        categories: {
          create: [{ category: { create: { name: 'News' } } }],
          connect: {
            categoryId_postId: { categoryId: 1, postId: ? }, // This doesn't work, even if I had the postId
          },
        },
      },
    });
  </code></pre>
  <p>How can I connect an existing category to a new post with the schema that I have?</p>