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

where_has not working #528

Closed
yubarajshrestha opened this issue Nov 29, 2021 · 4 comments
Closed

where_has not working #528

yubarajshrestha opened this issue Nov 29, 2021 · 4 comments
Labels
bug An existing feature is not working as intended

Comments

@yubarajshrestha
Copy link
Contributor

Users Table

id name
1 John
2 Jane

Roles Table

id name slug
1 Manager manager
2 Editor editor

RoleUser Pivot Table

id role_id user_id
1 2 1
2 2 2
users = User.where_has(
    'roles',
    lambda q: q.where('slug', 'editor')
).get()

Expected Output

[
    {
        "id": "1",
        "name": "John",
    }, {
        "id": "2",
        "name": "Jane",
    }
]

Current Output
Expected Output

[
    {
        "id": "1",
        "name": "John",
    }
]

Originally posted by @yubarajshrestha in #522 (comment)

@josephmancuso
Copy link
Member

could have swore I tested this exact scenario but I'll replicate this again 🤔

@josephmancuso josephmancuso added the bug An existing feature is not working as intended label Nov 29, 2021
@yubarajshrestha
Copy link
Contributor Author

could have swore I tested this exact scenario but I'll replicate this again 🤔

SELECT * FROM "users" WHERE EXISTS (SELECT "role_user".* FROM "role_user" WHERE "role_user"."id" = "users"."id" AND "role_user"."id" IN (SELECT "roles"."id" FROM "roles" WHERE "roles"."slug" = 'editor'))

I can see that you're comparing role_user.id with users.id which is incorrect.

@josephmancuso
Copy link
Member

josephmancuso commented Nov 30, 2021

hmm. correct query should be

SELECT * FROM "users" WHERE EXISTS (SELECT "role_user".* FROM "role_user" WHERE "role_user"."user_id" = "users"."id" AND "role_user"."role_id" IN (SELECT "roles"."id" FROM "roles" WHERE "roles"."slug" = 'editor'))

@josephmancuso
Copy link
Member

Fixed in #529

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An existing feature is not working as intended
Projects
Development

No branches or pull requests

2 participants