question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"every" relation filter not working as expected

See original GitHub issue

Bug 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:open
  • Created 2 years ago
  • Reactions:3
  • Comments:28 (10 by maintainers)

github_iconTop GitHub Comments

4reactions
Tronikeliscommented, Sep 26, 2022

@janpio

would be to add or do not ahve this relation set (= database NULL) or something similar to the explanation, did I understand that correctly?

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’t

4reactions
la40commented, Sep 12, 2022

The 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Solved: Relationship filtering on many to one doesnt filte...
Solved: Hi, I have several tables all linked to each other. What I am finding is that the filtering from many to one...
Read more >
Django queryset filter with Q object or relationship is not ...
I have a record A with text field "12345" and a b field as null. When trying to filter model A with: queryset...
Read more >
Relationship query filter not working - ACF Support
I'm trying to filter de results of a relationship field by modifying the query with he filter 'acf/fields/relationship/query'. Even when I add ...
Read more >
Why are the source filter conditions in my AWS DMS task not ...
There are several reasons why source filter conditions might not be working as expected on an AWS DMS task. For example, the engine...
Read more >
Power BI Relationships Not Working? Four Steps to Debug for ...
Are your Power BI Relationships not working ? It can be so frustrating when that happens. The good news is that the four...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found