Is there any way to update a relation? The follow...
# orm-help
y
Is there any way to update a relation? The following is the explicit many-to-many example from the docs:
Copy code
model Post {
  id         Int                 @id @default(autoincrement())
  title      String
  categories CategoriesOnPosts[]
}

model Category {
  id    Int                 @id @default(autoincrement())
  name  String
  posts CategoriesOnPosts[]
}

model CategoriesOnPosts {
  post       Post     @relation(fields: [postId], references: [id])
  postId     Int // relation scalar field (used in the `@relation` attribute above)
  category   Category @relation(fields: [categoryId], references: [id])
  categoryId Int // relation scalar field (used in the `@relation` attribute above)
  assignedAt DateTime @default(now())
  assignedBy String

  @@id([postId, categoryId])
}
Suppose I want to do the following:
Copy code
await prisma.post.update({
  where: {
    title: 'Foo'
  },
  data: {
    categories: {
      category: {
        name: 'Updated category name'
      }
    }
  }
});
All I get is `"error": "Unknown arg
categories
in where.categories for type PostWhereUniqueInput. Did you mean
set
?\n"` Is there any way for this to be accomplished?
r
@Yaakov I didn’t quite get what you’re trying to achieve. Do you want to connect the category to the post or do you want to update the category name?
y
@Ryan Update the (already connected) category's name. Thanks!
r
You can do it in this way:
Copy code
await prisma.post.update({
    where: { id: 1 },
    data: {
      categories: {
        update: {
          where: { postId_categoryId: { postId: 1, categoryId: 1 } },
          data: { category: { update: { name: 'new name' } } },
        },
      },
    },
  })
y
Thank you!!
👍 1