If I have a Post and I want to be able to see whic...
# orm-help
a
If I have a Post and I want to be able to see which users liked that post. It would make sense to store the id of each user that liked the post in an array, correct? Something like
Copy code
model Post {
  ...
  likes   Int      @default(0)
  likedBy String[]
}
r
You can do that or you can also self join it back to the User
I'll try find an example quick ..
🙏 1
a
Would you mind looking over my models? I'm working on an Instagram clone and I'd like to check that this makes sense before I continue

https://i.postimg.cc/RVFH0vqg/Screen-Shot-2020-07-21-at-6-25-14-AM.png

r
from
<https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-schema/relations#many-to-many-self-relations>
Copy code
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" ?
At a glance you can make
Post.likedBy
a
User
instead of
String
, same with
Comment.likedBy
Then when you query it you get to do:
Copy code
getPosts {
  id
  name
  likes
  likedBy {
    id
    username
  }
}
that kind of thing
a
likedBy _User_[] @relation("UserLiked", _references_: [id])
I got this error
Error parsing attribute "@relation": The relation field
likedBy
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
.
so I removed the reference id and it auto added in 2 fields to the User model
Copy code
post      Post?     @relation("UsersLiked", fields: [postId], references: [id])
  postId    String?
r
and did that work?
I'm not 100% comfortable with the "self join many to many" and I usually need to play around with it a bit before I get it right 😛
Yeah - the auto adding of the fields is fine ..
when you deploy that to your database see what gets created and if that makes sense..
The "Fields" it adds are usually foreign key constraints and not actual columns on the table..
a
So those fields added onto the user Model
wait nvm. Forgot what I was going to sya ...
😊 1
I'll read the link you posted, and try a few things out
r
Yeah .. I usually have to resort to that ..
The examples do work 100% - I can confirm that
a
the likedBy field is suppose to be on the Post model right?
r
It should make a new table because I think you'll have a Many to Many relationship?
A Users can like Many Posts and Posts can be liked by Many users
I get tripped by the wording..
@Ryan - can you cast some fresh eyes on this - I may have "eaten my tail" here ..
💯 1
🙃 2
r
@Awey @Richard Ward How about something like this?
Copy code
model 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
}
r
Thanks @Ryan but shouldn't the
User => @relation("likedPosts")
be on
likedPosts
and not
posts
?
👍 1
Then to add in Users liking comments it would be:
Copy code
model 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 🙂
💯 1
r
@Richard Ward yup. my bad 😅
a
@Richard Ward
likedComments
should be of type Comment[] not Post[] right?
r
aah yes - "copypaste"