Job.select().where(Job.user == current_user.id and Job.status == "Running")
Keeps returning the wrong row from the wrong user. It seems to ignore the statement Job.user == current_user.id
altogether.
I also tried
Job.select().join(User).where(Job.status == "Running")
same thing, it won't return the correct Job belonging to the current user.
The problem here is that and
doesn't translate to SQL AND
.
In fact, it can't do anything like that, in any library. The and
operator can't be overloaded, it short-circuits (doesn't evaluate the second argument) if the first isn't truthy, and it always returns the last thing it evaluated.
So, since Job.user == current_user.id
returns a non-empty query object, which is truthy, Job.user == current_user.id and Job.status == "Running"
returns Job.status == "Running"
. Which is why it's ignoring the current_user.id
.
Use the &
operator, or the bin_and
method, as the docs say.
Also, remember that &
doesn't have the same precedence as and
, so you will need parens around each comparison.
As for your second attempt, that join
works fine—it just means that each row in Job
is joined up with the columns from the corresponding row in User
. You haven't even tried to tell it what user you want restrict it to, and it can't read your mind.