'Data modeling for Prisma

I have an application that needs to model some data, but I'm having trouble getting Prisma to recognize the appropriate relationships when building out the tables in my prisma.schema file.

I am trying to create a global list of "Interests", and I want users to be able to add to this list as well as choose interests from it. When users add items to "Interests", I want to tie the creation of that interest to the User (createdBy: userId), allow it to be selected by other users, and when selected by other users, tie their profile Id to a list of profile id's within that interest.

model Profile {
  id     Int     @id @default(autoincrement())
  bio    String?
  user   User    @relation(fields: [userId], references: [id])
  userId Int     @unique
  interestList Interest[]

}

model User {
  id      Int      @id @default(autoincrement())
  email   String   @unique
  name    String?
  posts   Post[]
  profile Profile?
  interests Interest[] 
}

model Interest {
  id  Int @id @default(autoincrement())
  user User @relation(fields: [createdBy], references: [id]
  //Don't understand what relational fields to add/modify to make this happen...
  
}


Solution 1:[1]

If I understand correctly, you want to create 1:n relation for createdBy (so 1 user can create n interests, but interest can be created only by 1 user) and than relation n:m for or profile that selected this interest?

so this can look like this way

model Profile {
  id        Int        @id @default(autoincrement())
  bio       String?
  user      User       @relation(fields: [userId], references: [id])
  userId    Int        @unique
  interests Interest[]

}

model User {
  id       Int        @id @default(autoincrement())
  email    String     @unique
  name     String?
  profile  Profile?
  Interest Interest[]
}

model Interest {
  id         Int       @id @default(autoincrement())
  user       User      @relation(fields: [createdBy], references: [id])
  createdBy  Int
  selectedBy Profile[]
}

If you want to connect selectBy to User not Profile check this documentation https://www.prisma.io/docs/concepts/components/prisma-schema/relations#disambiguating-relations

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1