Hi guys, I am trying to add follower/following dat...
# orm-help
p
Hi guys, I am trying to add follower/following data to the db. What is the best approach to do it? It is throwing errors when I try adding relationship with the User model twice in the table.
r
@Prince ๐Ÿ‘‹ Could you share your models and how youโ€™re adding the data?
p
Copy code
// Main user model.
model User {
  id               String          @id @default(cuid())
  user_id          String          @unique
  first_name       String
  last_name        String
  email            String          @unique
  password         String
  email_verified   Boolean         @default(false)
  is_verified_user Boolean         @default(false)
  is_banned        Boolean         @default(false)
  profile          Profile?
  created_at       DateTime        @default(now())
  updated_at       DateTime        @updatedAt
  sign_up_metadata SignUpMetadata?
  user_sessions    UserSessions[]
  posts            Post[]
  recipes          Recipe[]
  media_files      MediaFile[]

  @@index([id, user_id])
  Follow Follow[]
}

// Contains user's profile data.
model Profile {
  id              String   @id @default(uuid())
  user            User     @relation(fields: [user_id], references: [id])
  desctiption     String   @default("")
  country         String?
  profile_picture String?
  user_id         String   @unique
  unit            Units    @default(METRIC)
  created_at      DateTime @default(now())
  updated_at      DateTime @updatedAt
}
This is where the user data is added. I tried adding the fields
following
and
followers
inside the
Profile
model and it didn't work.
r
This would be a better model to maintain follower/following:
Copy code
model User {
  id               String   @id @default(cuid())
  user_id          String   @unique
  first_name       String
  last_name        String
  email            String   @unique
  password         String
  email_verified   Boolean  @default(false)
  is_verified_user Boolean  @default(false)
  is_banned        Boolean  @default(false)
  profile          Profile?
  created_at       DateTime @default(now())
  updated_at       DateTime @updatedAt
  followers        User[]   @relation("follows")
  following        User[]   @relation("follows")

  @@index([id, user_id])
}

// Contains user's profile data.
model Profile {
  id              String   @id @default(uuid())
  user            User     @relation(fields: [user_id], references: [id])
  desctiption     String   @default("")
  country         String?
  profile_picture String?
  user_id         String   @unique
  created_at      DateTime @default(now())
  updated_at      DateTime @updatedAt
}
p
Thank you ๐Ÿ™‚
๐Ÿ‘ 1