Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#10432 closed (fixed)

Empty generator in "in" lookup gives incorrect SQL

Reported by: Pavel Anossov Owned by: Malcolm Tredinnick
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 Pavel Anossov 8 years ago.
a simple patch

Download all attachments as: .zip

Change History (5)

Changed 8 years ago by Pavel Anossov

Attachment: patch-10432-1.diff added

a simple patch

comment:1 Changed 8 years ago by Malcolm Tredinnick

Needs documentation: unset
Needs tests: unset
Owner: changed from nobody to Malcolm Tredinnick
Patch needs improvement: unset
Status: newassigned

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 8 years ago by Malcolm Tredinnick

Resolution: fixed
Status: assignedclosed

Fixed in r9986.

comment:3 Changed 8 years ago by Malcolm Tredinnick

(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 8 years ago by Pavel 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