Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prisma many-to-many query not properly structuring data #15643

Closed
demacericcardo opened this issue Oct 3, 2022 · 10 comments
Closed

Prisma many-to-many query not properly structuring data #15643

demacericcardo opened this issue Oct 3, 2022 · 10 comments
Labels
bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: broken query

Comments

@demacericcardo
Copy link

I can't get the data from a many to many relation in a proper way. I got the relation: USER-EVENT

Prisma query:

await this.prisma.event.findUnique({ where: { id: eventId }, include: { users: { select: { user: true } } } })

The data appears double nested:

MTEWq

@millsp millsp added team/client Issue for team Client. kind/bug A reported bug. bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. labels Oct 4, 2022
@millsp
Copy link
Member

millsp commented Oct 4, 2022

Hey @demacericcardo, could you please share the following with us?

  • Your schema
  • Node.js version
  • Prisma version
  • Some code for reproducing this

That would be helpful for us to be able to look into it. Thanks!

@demacericcardo
Copy link
Author

My schema:

`generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

username String @unique
email String
passwordHash String

name String
surname String
birthDate DateTime
profilePic String

roles Role[]
events UsersOnEvents[]
}

model Event {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

eventDate DateTime
title String
description String?
image String?

users UsersOnEvents[]
}

model UsersOnEvents {
user User @relation(fields: [userId], references: [id], onDelete: Cascade, onUpdate: Cascade)
userId Int
event Event @relation(fields: [eventId], references: [id], onDelete: Cascade, onUpdate: Cascade)
eventId Int
assignedAt DateTime @default(now())

@@id([userId, eventId])
}

enum Role {
User
Admin
}`

Node version: 16.16.0

Prisma version:
prisma : 4.3.0
@prisma/client : 4.3.1

The code is the same I already posted:

await this.prisma.event.findUnique({ where: { id: eventId }, include: { users: { select: { user: true } } } })

@millsp
Copy link
Member

millsp commented Oct 5, 2022

Thanks. The result data/structure seems expected to me, why is it unexpected for you?

@demacericcardo
Copy link
Author

I was expecting the query not including the data from the many to many table but just an array of the referenced users, how can i do that? if there's a way

@millsp
Copy link
Member

millsp commented Oct 7, 2022

If I understood well, you want the nested result as a top result? That's not possible. You'd need to get it yourself by accessing the nested properties.

@demacericcardo
Copy link
Author

Ok, no problem. Thank you

@luminous8
Copy link

luminous8 commented Oct 9, 2022

If I understood well, you want the nested result as a top result? That's not possible. You'd need to get it yourself by accessing the nested properties.

What's the best way to achieve that when quering multiple times the same field with a many-to-many relationship? @millsp
Could a middleware work to achieve what's suggested in this article automatically?

I'm refering to this block of code

const result = posts.map((post) => {
  return { ...post, tags: post.tags.map((tag) => tag.tag) }
})

@demacericcardo
Copy link
Author

Yeah i was about to cycle the data and restructure the object before using it, my issue was about if there was a better way with just a single query.
Thank you anyway

@millsp
Copy link
Member

millsp commented Oct 10, 2022

What's the best way to achieve that when quering multiple times the same field with a many-to-many relationship? @millsp
Could a middleware work to achieve what's suggested in this article automatically?

Middleware can do that. However, be aware that you'd lose type-safety. That might be ok if that isn't a requirement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: broken query
Projects
None yet
Development

No branches or pull requests

3 participants