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

user299709
  • 4,922
  • 10
  • 56
  • 88
  • Although I've never used PeeWee, the docs seem to mention the form `where((Job.user == current_user.id) & (Job.status == "Running"))` – Joachim Isaksson Sep 11 '14 at 04:33

1 Answers1

2

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.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • this is my biggest gripe about PeeWee. It takes more time to figure out how to do things PeeWee way than writing SQL query. – user299709 Sep 11 '14 at 04:39
  • @user299709: Unlike, say, SQLAlchemy, PeeWee's documentation seems to expect that you'll know a lot about Python and about ORMs in general. Probably because it's a small project mostly written by one guy. Given that, the docs are actually pretty impressive, and it's not that surprising that he neglects to document things that are obvious to him. But really, 90% of what people ask for _is_ clearly documented in one small section on [query operators](http://peewee.readthedocs.org/en/latest/peewee/querying.html#query-operators); I don't know why people have such a hard time finding it. – abarnert Sep 11 '14 at 04:42
  • I am not alone in finding PeeWee frustrating, there must be some underlying issue (like the prerequisite knowledge you talked about) in transition or introduction of such 'you should already know this so not gonna bring it up). I've used ORM for other languages but not once was it frustrating because the documentation had abundant example queries. I think really Peewee documentation can be improved more on by providing more exhaustive examples. – user299709 Sep 11 '14 at 04:53
  • @user299709: Well, it is an open source project, and you're obviously putting a lot of time (and effort and frustration) into learning it, and you know what it needs… have you consider submitting some examples to improve the docs, or at least filing docs bugs? (Even without that background, you could probably scour the examples from the tutorial for SQLAlchemy or C#/LINQ or many other prominent ORMs for many different languages and just file docs bugs for all the ones you can't figure out how to write in PeeWee by searching the docs… But it would probably be less work for you.) – abarnert Sep 11 '14 at 05:22
  • In defense of my documentation, there is a quickstart designed to show-case common features. It is listed right after installation in the docs and covers these "common cases" -- http://peewee.readthedocs.org/en/latest/peewee/quickstart.html. – coleifer Sep 11 '14 at 16:51
  • @coleifer: I think people have gotten used to not reading documentation even when they have a question that should be obviously covered in the documentation. Also, people publicly complain about documentation on open source projects and never once think that they could just file a bug and the author would almost certainly welcome it and improve things for everyone. And I have no idea what you can do about either of those. Anyway, take heart in the fact that these complaints wouldn't exist if your library weren't useful to all these people despite alternatives existing. :) – abarnert Sep 11 '14 at 17:29