Hi Prisma-Users! I have very basic question on ho...
# prisma-client
a
Hi Prisma-Users! I have very basic question on how to create joins the PRISMA-way. I did not find a clear guidance in the docs: Imagine the ER in the attachment. We have
areas
and
epics
. An
area
has many
epics
and they are related through an
areaId
on epics. I have a controller method
area/<area-uid>/epics
in my API. What’s the best way to get all epics for a given area ? I tried two ways: 1. Two function calls
Copy code
const area = await this.findOne(uid)
return this.prisma.epic.findMany({ where: { area } })
--> this creates two independent SQL quereies 2. One function call
Copy code
return (await this.prisma.area.findUnique({ where: { uid }, select: { epics: true } }))['epics']
Works, but feels quirky. --- Is there a better solution? Like a simple right join? Thanks for feedback. I am sure this is a rookie thing. Cheers Anton
m
Copy code
prisma.epic.findMany({
  where: {
    areaId: uid 
  }
});
😅 1
n
Hey Anton 👋 if I understand you correctly, a single query should be enough for your use case:
Copy code
const epicsForArea = await prisma.epic.findMany({
  where: {
    areaId: uid
  }
})
😂 1
Nice @Maciek K, you beat me to it 😄
m
@nikolasburk I saw it actually pop after I hit enter 😄
n
@Anton Reindl, for clarity, your ER diagram would look similar to this modelled as a Prisma schema:
Copy code
model Area {
  id          String  @id @default(uuid())
  label       String?
  description String?
  // ... more fields
  epics       Epic[]
}

model Epic {
  id        String    @id @default(uuid())
  title     String?
  startDate DateTime?
  endDate   DateTime?
  // ... more fields
  areaId    String
  area      Area      @relation(fields: [areaId], references: [id])
}
a
Thanks a lot guys! That is one way. But my ER image was wrong. We have a UUID on Area but that is not used for join. The Areas have UID and ID as attributes. Area and Epics join based on ID. That’s why we first have to find the Area based on UID before we can go on. So the proposal is to use the UID as the reference ID?
n
Could you maybe share the relevant parts of your Prisma schema? That way it would be easier to figure out the right Prisma Client query 🙂
a
Sure Epic:
Copy code
model Epic {
  id          Int      @id @default(autoincrement())
  uid         String   @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  cid         String?  @unique
  title       String   @db.VarChar
  description String?
  deadline    DateTime @db.Date


  outcome String? @db.Text
  details String? @db.Text

  successMeasurement String? @db.Text

  impact           String? @db.Text
  scoreDescription String? @db.Text


  tenantId String @db.VarChar
  kanban   Kanban @relation(fields: [kanbanId], references: [id])
  kanbanId Int

  type   EpicType? @relation(fields: [typeId], references: [id])
  typeId Int?

  owner   User? @relation(fields: [ownerId], references: [id])
  ownerId Int?

  team UsersOnEpics[]

  area   Area @relation(fields: [areaId], references: [id])
  areaId Int
 

 


}
Area:
Copy code
model Area {
  id          Int          @id @default(autoincrement())
  uid         String       @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  cid         String?      @unique
  title       String       @db.VarChar
  description String?
  leitmotif   String?      @db.VarChar
  type        AreaTypeEnum @default(company)
  tenantId    String       @db.VarChar

  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt
  deletedAt DateTime?
  createdBy String?   @db.VarChar

  owner   User? @relation(fields: [ownerId], references: [id])
  ownerId Int?

  epics Epic[]


  @@index([id, uid, cid, tenantId])
}
n
I see, if you’re looking to join by the
uid
then you should be able to adjust the query that Maciek and I shared like so:
Copy code
const epicsForArea = await prisma.epic.findMany({
  where: {
    uid: uid
  }
})
Does this work now? 🙂
a
Will test, but i am very sure it will work
n
Let us know if you run into any further issues 🙌