2021年5月2日星期日

Prisma: Create or Connect Records in Explicit Many-to-Many Relations

In my Prisma Schema, I'm finding it difficult to undertand how to to create records in case of explicit many-to-many relations.

I have the following schema. Basically it represents Lists of Books. Users can Create Lists of Books.

A user can create a New list and then add books to this list along with their own notes. The Book Model is pure and contains standard book information.

The extra model is required because the user who is adding the book to the list can add his own notes about the book.

model List {    id        Int     @default(autoincrement()) @id    title     String    slug      String?    content   String?    published Boolean @default(false)    author    User?   @relation(fields: [authorId], references: [id])    authorId  Int?    books     BooksInLists[]    createdAt DateTime @default(now())    updatedAt DateTime @updatedAt  }    model BooksInLists {    list        List     @relation(fields: [listId], references: [id])    listId      Int      // relation scalar field (used in the `@relation` attribute above)    book    Book @relation(fields: [bookId], references: [id])    bookId  Int      // relation scalar field (used in the `@relation` attribute above)    @@id([listId, bookId])    adder    User?   @relation(fields: [adderId], references: [id])    adderId  Int?    notes   String?  }    model Book {    id     Int     @id @default(autoincrement())    name   String    lists  BooksInLists[]    curator    User?   @relation(fields: [curatorId], references: [id])    curatorId  Int?    bookDescription  String?  }    model User {    id            Int       @default(autoincrement()) @id    name          String?    email         String?   @unique    lists         List[]    books         Book[]    booksinlists  BooksInLists[]      @@map(name: "users")  }  

The queries that I want to be able to do.

  • While updating a list, I should be able to add a new book to the list. This should create the new book, and also allow me to add a new record in BooksInLists model along with the 'notes' field.

  • While updating a list, I should be able to add/connect an existing book to the list. This will allow me to add a new record in BooksInLists model along with the 'notes' field.

https://stackoverflow.com/questions/67362724/prisma-create-or-connect-records-in-explicit-many-to-many-relations May 03, 2021 at 10:04AM

没有评论:

发表评论