"every" relation filter not working as expected
See original GitHub issueBug description
Problem 0: “every” relation filter return all the admins that has no permissions at all. (in the n-to-n table exists no records relating this admins with some permission) This admins should not be returned because they has no permissions and the every condition is not completed.
Problem 1 (Query 1): This query return all the admins that have no permissions + all the admins that have permission 1, or 2, or 3, or 1+2+3. This is copletely wrong. It should return only the admins having 1+2+3.
Problem 2 (Query 2): This query is working partially correct returning not the 1,2,3 admins because they don’t complete the every condition but still return all the admins that have no related permissions which is obviously wrong.
How to reproduce
Follow the primsa information below.
Expected behavior
“Every” should return only the admins that complete the condition agains every relation and should ignore the admins that haven’t relations.
“Every + in” should return only admins compliting the all “every” conditions but it works like some and also return all the admins having not related permissions.
Prisma information
Schema
model Admin{
@@map("admins")
id Int @id @default(autoincrement())
email String @unique
name String?
permissions Permission[]
}
model Permission{
@@map("permissions")
id Int @id @default(autoincrement())
slug String
name String
category String
admins Admin[]
}
Query 1
prisma.admin.findMany({
select: {
id: true,
name: true,
email: true,
roles: {
select: {name: true},
},
permissions: {
select: {name: true},
},
},
where: {
permissions: {
every: {
id: {
in: [1, 2, 3]
}
}
}
}
})
Query 2
prisma.admin.findMany({
select: {
id: true,
name: true,
email: true,
roles: {
select: {name: true},
},
permissions: {
select: {name: true},
},
},
where: {
permissions: {
every: {
id: {
gt: 2
}
}
}
}
})
DB
#admins
1 admin@test.com admin
2 admin1@test.com admin1
3 admin2@test.com admin2
4 admin3@test.com admin3
5 admin4@test.com admin4
6 admin5@test.com admin5
#permissions
1 test-1 test 1 test_1
2 test-2 test 2 test_2
3 test-3 test 3 test_3
#_AdminsToPermissions
1 1
1 2
1 3
2 2
3 3
3 1
Environment & setup
- OS: Ubuntu
- Database: PostgresSQL
- Node.js version: v16.14.0
Prisma Version
@prisma/client": "^3.7.0
prisma": "^3.7.0
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:28 (10 by maintainers)
Top GitHub Comments
@janpio
Yes, because I would assume that the majority of people wouldn’t anticipate that
every
would return records with no relations. I for sure didn’tThe question is exactly about “in” + “every” (read my first comment) and that it doesn’t work the way people expect and may cause unexpected bugs. At the end I don’t use prisma anymore because I like to have control and to work logical. If you think that it is logically correct the “in” + “every” to return also records having no relations at all and should work in this way then it’s not a bug and you should close the issue. I have nothing more to say.