mongodbdatabase-designmany-to-manyprismadatabase-management

Struggling with Prisma schema setup for many-to-many relationships on MongoDB


I'm trying to create schemas using PrismaORM with MongoDB, and I'm facing an issue with establishing a many-to-many relationship between User and Post models. My goal is to establish a relation between favorites and favoriteBy fields in these models. However, I'm facing errors in achieving this setup within the Prisma schema syntax while using MongoDB.

Error - Error parsing attribute "@relation": The scalar field defined in the fields argument must be an array of the same type defined in references.enter image description here

Here is my complete Schema file:

datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["relationJoins"]
}

model User {
  id           String   @id @default(auto()) @map("_id") @db.ObjectId
  name         String?
  email        String   @unique
  username     String   @unique
  password     String
  profileImage String?  @default("https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_640.png")
  bio          String?
  createdAt    DateTime @default(now())
  updatedAt    DateTime @updatedAt

  post      Post[] @relation("UserPosts")
  favorites Post[] @relation("UserFavorites", references: [id])
}

model Post {
  id      String   @id @default(auto()) @map("_id") @db.ObjectId
  slug    String[]
  caption String?

  userId String @db.ObjectId
  user   User   @relation("UserPosts", fields: [userId], references: [id], onDelete: Cascade)

  favoritedBy User[] @relation("UserFavorites", fields: [userId], references: [id])

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

Can somebody help me fix this problem?

I attempted setting up a many-to-many relationship between User and Post models in Prisma schema with MongoDB in multiple ways and asked the error to ChatGPT as well, but couldn't find a solution. Hope, I can get my solution here.


Solution

  • Prisma's documentation covers this exact case with some very good examples so I recommend going over it. As mentioned in the docs, MongoDB doesn't support implicit many-to-many relations, so what you have to do is make it an explicit relation; this means adding a link table.

    Here's a complete example that should work for your use case:

    model User {
      id           String      @id @default(auto()) @map("_id") @db.ObjectId
      name         String?
      email        String      @unique
      username     String      @unique
      password     String
      profileImage String?     @default("https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_640.png")
      bio          String?
      createdAt    DateTime    @default(now())
      updatedAt    DateTime    @updatedAt
      /// Reference the link table
      userPosts    UserPosts[]
    }
    
    model Post {
      id        String      @id @default(auto()) @map("_id") @db.ObjectId
      slug      String[]
      caption   String?
      createdAt DateTime    @default(now())
      updatedAt DateTime    @updatedAt
      /// Reference the link table
      userPosts UserPosts[]
    }
    
    /// Link table
    model UserPosts {
      id     String @id @default(auto()) @map("_id") @db.ObjectId
      user   User   @relation(fields: [userId], references: [id])
      userId String @db.ObjectId
      post   Post   @relation(fields: [postId], references: [id])
      postId String @db.ObjectId
    }