Is it possible to filter nested queries based on partial top-level results? #11646
-
|
For example, suppose you have a one-to-many relationship between schools and classes (a school has many classes; a class belongs to one school). I have the ID of some class and what I want is to query that class, its school, and all classes that are in that school and that are from the same year as the class I have the ID for: prisma.db.class.findUnique({
select: {
year: true,
name: true,
school: {
select: {
name: true,
classes: {
// How do I only get classes in the same year as the top-level class?
}
}
}
},
where: { id: `some-class-id` }
})I think the problem here is that I don't know what the top-level class's year is in advance so I can't filter the results by the year. This is how I would do it in SQL: select
MainClass.year,
MainClass.name mainClassName,
School.name as schoolName,
SchoolClass.id as schoolClassId
-- etc
from Class MainClass
left join School on MainClass.schoolId = School.id
left join Class SchoolClass on SchoolClass.schoolId = School.id and SchoolClass.year = MainClass.year
where MainClass.id = "some-class-id"Am I missing something? Is this possible without raw SQL? If this is not possible, then it seems I have three options right?
Also, if there isn't already a feature request for this, then I would be happy to file one. Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
|
Hey @TomerAberbach 👋 , This is not possible yet with the Prisma Client, though we have an outstanding feature request #5048. The 3 possible options you outlined are the same I thought of, so I would move forward with one of those for now. You could also add your specific use case to #5048 and it will help our engineers as they look to design potential solutions! |
Beta Was this translation helpful? Give feedback.
-
|
Hey, did you find a solution to this? I'm trying to use the results of the nested query to filter the solutions as this: super(prisma.team_Member, {
include: {
Team: {
include: {
Challenges: {
include: {
Challenge:{
select:{
Stages:{
select:{
Solutions: {
where:{
ID_Team: { equals: prisma.team.fields.ID}
}
},
}
}
}
},
}
}
},
},
},
});But it is retrieving all the solutions per stage rather than filtering by the Team Models: |
Beta Was this translation helpful? Give feedback.
-
|
Hi there, To keep our discussions organized and focused on the most relevant topics, we’re reviewing and tidying up our backlog. As part of this process, we’re closing discussions that have already been marked as answered but remain open. If this discussion still requires further input or clarification, feel free to reopen it or start a new one with updated details. Your contributions are invaluable to the community, and we’re here to help! For more details about our priorities and vision for the future of Prisma ORM, check out our latest blog post: https://www.prisma.io/blog/prisma-orm-manifesto. Thank you for your understanding and ongoing support of the Prisma community! |
Beta Was this translation helpful? Give feedback.
Hey @TomerAberbach 👋 ,
This is not possible yet with the Prisma Client, though we have an outstanding feature request #5048. The 3 possible options you outlined are the same I thought of, so I would move forward with one of those for now.
You could also add your specific use case to #5048 and it will help our engineers as they look to design potential solutions!