Opened 16 years ago

Closed 16 years ago

Last modified 16 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: dev
Severity: Keywords: sql where
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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 16 years ago.
a simple patch

Download all attachments as: .zip

Change History (5)

by Pavel Anossov, 16 years ago

Attachment: patch-10432-1.diff added

a simple patch

comment:1 by Malcolm Tredinnick, 16 years ago

Owner: changed from nobody to Malcolm Tredinnick
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 by Malcolm Tredinnick, 16 years ago

Resolution: fixed
Status: assignedclosed

Fixed in r9986.

comment:3 by Malcolm Tredinnick, 16 years ago

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

in reply to:  1 comment:4 by Pavel Anossov, 16 years ago

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