6

With Peewee I'm trying to use limit as follows:

one_ticket = Ticket.select().limit(1)
print one_ticket.count()

This prints out 5 however. Does anybody know what's wrong here?

kramer65
  • 50,427
  • 120
  • 308
  • 488

1 Answers1

2

Try running peewee in debug mode, which is actually just setting up the python logging module to handle logging.DEBUG level items:

import logging
logging.basicConfig(
    format='[%(asctime)-15s] [%(name)s] %(levelname)s]: %(message)s',
    level=logging.DEBUG
)

# From here, you can now perform your query, and you should see peewee's debug output using the logging module.
one_ticket = Ticket.select().limit(1)
print one_ticket.count()

Ideally, you should see the raw query.

VooDooNOFX
  • 4,674
  • 2
  • 23
  • 22
  • Thanks for the tip. The raw query is as follows: `[2013-11-19 10:41:33,555] [peewee] DEBUG]: ('SELECT Count(t1."id") FROM "ticket" AS t1 LIMIT 1', [])`. This still leaves me clueless as to why the count is 5 though. Any ideas? – kramer65 Nov 19 '13 at 09:43
  • You likely need to use `one_ticket.wrapped_count()` instead. The more direct `one_ticket.count()` will default to counting the number of items from primary key, as seen in the source: https://github.com/coleifer/peewee/blob/master/peewee.py#L1653 – VooDooNOFX Nov 19 '13 at 09:48
  • Unfortunately, wrapped_count() also results in 5. Also, it would be quite weird if the count-method on the result of a query doesn't count the results, but does a new query. (I really appreciate you trying to help me out though! :) ) Would you have any other ideas? – kramer65 Nov 19 '13 at 09:55
  • Unfortunately, i'm spent. Last idea, update peewee to the latest version from github and try again. Perhaps it's a transient error? – VooDooNOFX Nov 19 '13 at 21:22
  • @coleifer - I was just rereading over this Q&A, and hit upon your comment. May I ask; why does peewee clear limits when doing a count? That seems totally unintuitive behaviour to me.. – kramer65 Feb 12 '15 at 07:25
  • I'm way late to this discussion, but maybe this will help a new learner. The reason the SQL query returns 5 is that there are five items in the "ticket" table and that count of 5 can be returned as a single row in the SQL response. The LIMIT is just for the number of rows returned. This is how SQL works and has nothing to do with Peewee. – Kenny Pyatt Aug 02 '23 at 15:41