Hi everyone, I need some help on my data modelisat...
# orm-help
a
Hi everyone, I need some help on my data modelisation, what i'm trying to do is : • Reference a group ID in User model. A user can only be in one group, this is what i've done below • Reference in Group Model all users that are inside a group. This is what I need help with.
Copy code
model User {
  id         String   @id @default(uuid())
  email      String   @unique
  role       Role     @default(USER)
  group      Group?   @relation(fields: [group_id], references: [id], onDelete: SetNull) 
  group_id   String   
  created_at DateTime @default(now())
  updated_at DateTime @updatedAt

  @@map("user")
}

model Group {
  id         String   @id @default(uuid())
  name       String
  users      User[]
  created_at DateTime @default(now())
  updated_at DateTime @updatedAt
  deleted    Boolean  @default(false)

  @@map("group")
}
How can I make this list of User when I already have users User[] for the relation in User model ? (BTW i've read the doc but i'm totally lost...) Thanks a lot !
r
@A DSJ šŸ‘‹ You already have the relation for all users that are inside a group:
Copy code
users      User[]
This is what will make sure that all the users are in a group. Is there anything else that you were looking for?
a
Thanks for your answer, i think I got lost as I thought this was kind of a helper for the first relation I made inside User. So I was trying to tell prisma that I want a list of string (uuid) inside group as well but it does that automatically
but in my DB (postgres) i do not see a list of Users in my groupe table, even though the group is registered in my user
r
You won't see a list in the group table, you will see the group_id in the user table.
The fetching and grouping of users is done via the foreign key group_id and shown. The group table in the database will not have any users explicitly.
a
Oh ok, so there is no use to create that kind of entry as I should be able to fetch all foreign key group_id that are same right ?
šŸ’Æ 1
I see the relation in prisma studio, thanks a lot for taking time answering my noob questions, this helped me a lot, prisma is amazing !
šŸ™Œ 1
r
Happy to help šŸ™‚