Awey
07/21/2020, 12:18 PMmodel Post {
...
likes Int @default(0)
likedBy String[]
}Richard Ward
07/21/2020, 12:22 PMRichard Ward
07/21/2020, 12:22 PMAwey
07/21/2020, 12:25 PMAwey
07/21/2020, 12:25 PMhttps://i.postimg.cc/RVFH0vqg/Screen-Shot-2020-07-21-at-6-25-14-AM.png▾
Richard Ward
07/21/2020, 12:26 PM<https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-schema/relations#many-to-many-self-relations>
model User {
id Int @id @default(autoincrement())
name String?
followedBy User[] @relation("UserFollows", references: [id])
following User[] @relation("UserFollows", references: [id])
}
Just changed the "followedBy" to "likedBy" ?Richard Ward
07/21/2020, 12:29 PMPost.likedBy a User instead of String, same with Comment.likedBy
Then when you query it you get to do:
getPosts {
id
name
likes
likedBy {
id
username
}
}
that kind of thingAwey
07/21/2020, 12:30 PMlikedBy _User_[] @relation("UserLiked", _references_: [id])Awey
07/21/2020, 12:31 PMAwey
07/21/2020, 12:31 PMlikedBy on Model Post must not specify the fields or references argument in the @relation directive. You must only specify it on the opposite field Post on model User.Awey
07/21/2020, 12:32 PMAwey
07/21/2020, 12:32 PMpost Post? @relation("UsersLiked", fields: [postId], references: [id])
postId String?Richard Ward
07/21/2020, 12:32 PMRichard Ward
07/21/2020, 12:33 PMRichard Ward
07/21/2020, 12:33 PMRichard Ward
07/21/2020, 12:34 PMRichard Ward
07/21/2020, 12:34 PMAwey
07/21/2020, 12:35 PMAwey
07/21/2020, 12:35 PMAwey
07/21/2020, 12:35 PMRichard Ward
07/21/2020, 12:36 PMRichard Ward
07/21/2020, 12:36 PMAwey
07/21/2020, 12:48 PMRichard Ward
07/21/2020, 12:56 PMRichard Ward
07/21/2020, 12:57 PMRichard Ward
07/21/2020, 12:57 PMRichard Ward
07/21/2020, 12:59 PMRyan
07/22/2020, 7:07 AMmodel User {
id Int @id @default(autoincrement())
email String @unique
username String @unique
posts Post[] @relation("likedPosts")
likedPosts Post[]
comments Comment[]
createdAt DateTime @default(now())
}
model Post {
id Int @id @default(autoincrement())
description String
user User @relation(fields: [userId], references: [id])
likedByUsers User[] @relation("likedPosts")
comments Comment[]
createdAt DateTime @default(now())
userId Int
}
model Comment {
id Int @id @default(autoincrement())
text String
post Post @relation(fields: [postId], references: [id])
user User @relation(fields: [userId], references: [id])
createdAt DateTime @default(now())
postId Int
userId Int
}Richard Ward
07/22/2020, 7:51 AMUser => @relation("likedPosts") be on likedPosts and not posts ?Richard Ward
07/22/2020, 7:56 AMmodel User {
id Int @id @default(autoincrement())
email String @unique
username String @unique
posts Post[]
likedPosts Post[] @relation("likedPosts")
likedComments Post[] @relation("likedComments")
comments Comment[]
createdAt DateTime @default(now())
}
model Post {
id Int @id @default(autoincrement())
description String
user User @relation(fields: [userId], references: [id])
likedByUsers User[] @relation("likedPosts")
likes Int @default(0)
comments Comment[]
createdAt DateTime @default(now())
userId Int
}
model Comment {
id Int @id @default(autoincrement())
text String
post Post @relation(fields: [postId], references: [id])
user User @relation(fields: [userId], references: [id])
likedByUsers User[] @relation("likedComments")
likes Int @default(0)
createdAt DateTime @default(now())
postId Int
userId Int
}
I'm going to start collecting these snippets 🙂Ryan
07/22/2020, 8:40 AMAwey
07/22/2020, 12:58 PMlikedComments should be of type Comment[] not Post[] right?Richard Ward
07/22/2020, 12:58 PM