is there a way to make this work? Relationship bet...
# orm-help
b
is there a way to make this work? Relationship between
authorId
and
followingId
?
Copy code
model Users {
  id BigInt @id @default(autoincrement())

  followers  Follows[] @relation("follower")
  followings Follows[] @relation("following")

  posts          Posts[]

  createdOn DateTime @default(now()) @map("created_on")
  updatedOn DateTime @updatedAt @map("updated_on")

  @@map("users")
}

// Following users can have many posts
model Follows {
  id BigInt @default(autoincrement())

  followerId  BigInt @map("follower_id")
  followingId BigInt @map("following_id")

  follower  Users @relation("following", fields: [followerId], references: [id])
  following Users @relation("follower", fields: [followingId], references: [id])

  followingPosts Posts[]

  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @default(now()) @map("updated_at")
  @@id([id, followerId, followingId])
  @@map("follows")
}

// Posts will always have an authorId
model Posts {
  id     BigInt  @default(autoincrement())
 
  authorId BigInt @map("author_id")
  author   Users  @relation(fields: [authorId], references: [id])

  follows Follows @relation(fields: [authorId], references: [followerId])

  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @default(now()) @map("updated_at")
  @@id([id, authorId])
  @@map("bits")
}

// Query should work like this 

users.findMany({
  where: {
    id: 1
  },
  include: {
    followings: { // all posts of the users that I follow
      include: {
        posts: true
      }
    }
  }
})
n
When I used this schema, I got this error (image), have you added
map
attribute in the relation fields like this?
Copy code
// Posts will always have an authorId
model Posts {
  id        BigInt   @default(autoincrement())
  authorId  BigInt   @map("author_id")
  author    Users    @relation(fields: [authorId], references: [id], map: "author")
  follows   Follows  @relation(fields: [authorId], references: [followerId], map: "follows")
  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @default(now()) @map("updated_at")

  @@id([id, authorId])
  @@map("bits")
}