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

Is it possible to select table data when doing a multiple-join without mentioning every selected table? #787

Closed
perplexedfingers opened this issue Dec 8, 2015 · 3 comments

Comments

@perplexedfingers
Copy link

The following query does not work.

(PermissionInGroup
      .select(PermissionInGroup, Permission, Group)
      .join(Group)
      .where(Group.app_account == app_account_id,
                Group.company_id == company_id)
      .join(UserInGroup)
      .join(User)
      .where(User.ID == user_id)
       )

SQL statement would be like

SELECT "t1"."id", "t1"."permission_id", "t1"."group_id", "t5"."name", "t4"."primary_key", "t4"."name", "t4"."company_id", "t4"."app_account_id"
FROM "permissioningroup" AS t1
INNER JOIN "group" AS t4 ON ("t1"."group_id" = "t4"."primary_key")
INNER JOIN "useringroup" AS t2 ON ("t4"."primary_key" = "t2"."group_id")
INNER JOIN "user" AS t3 ON ("t2"."user_id" = "t3"."ID")
WHERE ((("t4"."app_account_id" = ?) AND ("t4"."company_id" = ?)) AND ("t3"."ID" = ?))
[722, 25, 32]

I will have this exception while trying to iterate over it

peewee.OperationalError: no such column: t5.name

But this one works.

(PermissionInGroup
           .select(PermissionInGroup, Permission, Group)
           .join(Group)
           .where(Group.app_account == app_account_id,
                      Group.company_id == company_id)
           .join(UserInGroup)
           .join(User)
           .where(User.ID == user_id)
           ####### mark the difference
           .switch(PermissionInGroup)
           .join(Permission)
           .order_by(Permission.name)
           #######
           )

SQL statement

SELECT "t1"."id", "t1"."permission_id", "t1"."group_id", "t5"."name", "t4"."primary_key", "t4"."name", "t4"."company_id", "t4"."app_account_id"
FROM "permissioningroup" AS t1
INNER JOIN "group" AS t4 ON ("t1"."group_id" = "t4"."primary_key")
####### mark the difference
INNER JOIN "permission" AS t5 ON ("t1"."permission_id" = "t5"."name")
#######
INNER JOIN "useringroup" AS t2 ON ("t4"."primary_key" = "t2"."group_id")
INNER JOIN "user" AS t3 ON ("t2"."user_id" = "t3"."ID")
WHERE ((("t4"."app_account_id" = ?) AND ("t4"."company_id" = ?)) AND ("t3"."ID" = ?))
####### mark the difference
ORDER BY "t5"."name"
#######
[722, 25, 32]

Can I have a better way to do this query?
I don't need to do order_by.

@coleifer
Copy link
Owner

coleifer commented Dec 9, 2015

You have to join on permission since you're selecting from it. That will never change.

If you don't need the order by, why are you calling it? I'm confused why that's an issue.

@perplexedfingers
Copy link
Author

It requires an action after join().

@perplexedfingers
Copy link
Author

I figure it out. Sorry about wasting your time.

(PermissionInGroup.select(Permission.name,
                          Group.name,
                          PermissionInGroup)
                                .join(Permission)
                                .switch(PermissionInGroup)
                                .join(Group)
                                .join(UserInGroup)
                                .join(User)
                                .where(User.ID == user_id,
                                       Group.app_account_id == app_account_id,
                                       Group.company_id == company_id)
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants