We have two tables: Users and Permissions
We want to select all the users who do NOT have a "guest" permission. Now, it is possible for users to have multiple permissions (not just 1) so simply querying for !"guest" won't work. Here is what our query looks like now:
query = session.query(Users).join(Permission, and_(
Permission.userId == theUser.uid, Permission.deviceId== theDevice.uid))
query.join(Permission).filter(~exists().where(and_(Permission.level==SqlConstants.PermissionLevels.GUEST, Users.uid == Permission.userId)))
I'm not sure if the join in the first line is relevant to the problem we are having, but we are using it, so I'm including it here. (I'll edit it out if it isn't relevant.)
The above returns the following exception:
returned no FROM clauses due to auto-correlation; specify correlate(<tables>) to control correlation manually.
I gleaned this pattern from the following SO post: Using NOT EXISTS clause in sqlalchemy ORM query
as well as from the sqlalchemy documentation (which is shallow concerning not exists): http://docs.sqlalchemy.org/en/rel_1_0/orm/query.html
It isn't clear to me what I'm doing wrong or whether there is a better way.