Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#10432 closed (fixed)

Empty generator in "in" lookup gives incorrect SQL

Reported by: Anossov Owned by: mtredinnick
Component: Database layer (models, ORM) Version: master
Severity: Keywords: sql where
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Simple story, here's what happens:

> User.objects.filter(pk__in=[]) # ok
[]

> User.objects.filter(pk__in=(x for x in [])).query.as_sql() # ProgrammingError
(...)
ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 21' at line 1")

> User.objects.filter(pk__in=(x for x in [])).query.as_sql()
(u'SELECT (...) FROM `user` WHERE `user`.`id` IN ()',
 ())

A patch is included.

Attachments (1)

patch-10432-1.diff (539 bytes) - added by Anossov 6 years ago.
a simple patch

Download all attachments as: .zip

Change History (5)

Changed 6 years ago by Anossov

a simple patch

comment:1 follow-up: Changed 6 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to mtredinnick
  • Patch needs improvement unset
  • Status changed from new to assigned

That patch doesn't look like it's patching the right thing: it looks too deep in the code. I notice that this bug is somehow specific to a generator being used in the example, since using a list comprehension (or any other sequence) does't demonstrate the problem.

For next time, too, a test case is definitely required in a patch for anything like this. After all, we need to make sure it doesn't reoccur for any reason.

comment:2 Changed 6 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from assigned to closed

Fixed in r9986.

comment:3 Changed 6 years ago by mtredinnick

(In [9987]) [1.0.X] Fixed #10432 -- Handle all kinds of iterators in queryset filters.

Only consumes the iterators once and works with Python 2.3.

Backport of r9986 from trunk.

comment:4 in reply to: ↑ 1 Changed 6 years ago by Anossov

Replying to mtredinnick:

I notice that this bug is somehow specific to a generator being used in the example, since using a list comprehension (or any other sequence) does't demonstrate the problem.

That's why it says "generator", not "sequence" in the title ;) bool(<any generator object> is True, bool([sequence with len=0]) is False

Note: See TracTickets for help on using tickets.
Back to Top