Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
DatabaseError with empty IN clause with Paginator
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Chi Ho Kwok  
View profile  
 More options Nov 8 2012, 8:44 am
From: Chi Ho Kwok <chk...@digibites.nl>
Date: Thu, 8 Nov 2012 05:44:16 -0800 (PST)
Local: Thurs, Nov 8 2012 8:44 am
Subject: DatabaseError with empty IN clause with Paginator

Hi devs,

I've noticed that after upgrading to Django 1.4, __in queries really don't
like empty sets. Simple queries still work, like
User.objects.filter(groups__in=[]), but most failures I've seen are with
Paginators. I think this is the minimum set to cause a DatabaseError,
create any app, add a models.py with:

from django.db import models

class Author(models.Model):
    pass

class Book(models.Model):
    author = models.ForeignKey(Author)

def crash():
    from django.core.paginator import Paginator

    pages = Paginator(Author.objects.all(), 25)
    page = pages.page(1)

    books = Book.objects.filter(author__in=page.object_list)
    print books

calling crash() will cause this stack trace:

C:\Workspace\someproject\src\someproject\test.py in <module>()
      6
      7 books = Book.objects.filter(author__in=page.object_list)
----> 8 print books
      9

C:\Dev\Python27\lib\site-packages\django\db\models\query.pyc in
__repr__(self)
     70
     71     def __repr__(self):
---> 72         data = list(self[:REPR_OUTPUT_SIZE + 1])
     73         if len(data) > REPR_OUTPUT_SIZE:
     74             data[-1] = "...(remaining elements truncated)..."

C:\Dev\Python27\lib\site-packages\django\db\models\query.pyc in
__len__(self)
     85                 self._result_cache = list(self.iterator())
     86         elif self._iter:
---> 87             self._result_cache.extend(self._iter)
     88         if self._prefetch_related_lookups and not
self._prefetch_done:
     89             self._prefetch_related_objects()

C:\Dev\Python27\lib\site-packages\django\db\models\query.pyc in
iterator(self)
    289             klass_info = get_klass_info(model, max_depth=max_depth,
    290                                         requested=requested,
only_load=only_load)
--> 291         for row in compiler.results_iter():
    292             if fill_cache:
    293                 obj, _ = get_cached_row(row, index_start, db,
klass_info,

C:\Dev\Python27\lib\site-packages\django\db\models\sql\compiler.pyc in
results_iter(self)
    761         if self.query.select_for_update and
transaction.is_managed(self.using):
    762             transaction.set_dirty(self.using)
--> 763         for rows in self.execute_sql(MULTI):
    764             for row in rows:
    765                 if resolve_columns:

C:\Dev\Python27\lib\site-packages\django\db\models\sql\compiler.pyc in
execute_sql(self, result_type)
    816
    817         cursor = self.connection.cursor()
--> 818         cursor.execute(sql, params)
    819
    820         if not result_type:

C:\Dev\Python27\lib\site-packages\django\db\backends\util.pyc in
execute(self, sql, params)
     38         start = time()
     39         try:
---> 40             return self.cursor.execute(sql, params)
     41         finally:
     42             stop = time()

C:\Dev\Python27\lib\site-packages\django\db\backends\postgresql_psycopg2\ba se.pyc
in execute(self, query, args)
     50     def execute(self, query, args=None):
     51         try:
---> 52             return self.cursor.execute(query, args)
     53         except Database.IntegrityError, e:
     54             raise utils.IntegrityError,
utils.IntegrityError(*tuple(e)), sys.exc_info()[2]

DatabaseError: syntax error at or near ")"
LINE 1: ...ugtest_book" WHERE "bugtest_book"."author_id" IN () LIMIT 21

The SQL statement created is:

SELECT "bugtest_book"."id", "bugtest_book"."author_id" FROM "bugtest_book"
WHERE "bugtest_book"."author_id" IN () LIMIT 21

We've worked around this on a few spots, but I don't think this is
intended, right?

Kind regards,

Chi Ho Kwok


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Russell Keith-Magee  
View profile  
 More options Nov 8 2012, 6:31 pm
From: Russell Keith-Magee <russ...@keith-magee.com>
Date: Fri, 9 Nov 2012 07:31:12 +0800
Local: Thurs, Nov 8 2012 6:31 pm
Subject: Re: DatabaseError with empty IN clause with Paginator

On Thu, Nov 8, 2012 at 9:44 PM, Chi Ho Kwok <chk...@digibites.nl> wrote:

you've definitely found a bug. If you can log this in Trac (and mark it as
a release blocker, since it's a change in behaviour), we can investigate
further.

Yours,
Russ Magee %-)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chi Ho Kwok  
View profile  
 More options Nov 9 2012, 7:06 am
From: Chi Ho Kwok <chk...@digibites.nl>
Date: Fri, 9 Nov 2012 04:06:32 -0800 (PST)
Local: Fri, Nov 9 2012 7:06 am
Subject: Re: DatabaseError with empty IN clause with Paginator

On Friday, 9 November 2012 00:31:39 UTC+1, Russell Keith-Magee wrote:
> If you can show a clear difference in behaviour between 1.3 and 1.4, then
> you've definitely found a bug. If you can log this in Trac (and mark it as
> a release blocker, since it's a change in behaviour), we can investigate
> further.

> Yours,
> Russ Magee %-)

I've traced the change back, but it doesn't seem to be introduced at 1.4.
On 1.2, this works:

C:\Dev\django\bugs>manage.py shell
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)

>>> from bugtest.models import *
>>> crash()
[]
>>> import django
>>> django.VERSION

(1, 2, 0, 'final', 0)


On 1.3.4:

C:\Dev\django\bugs>manage.py shell
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)

>>> from bugtest.models import *
>>> crash()

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Dev\django\bugs\bugtest\models.py", line 16, in crash
    print books
  File
"C:\Dev\Python27\lib\site-packages\django-1.3.4-py2.7.egg\django\db\models\ query.py",
line 69, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File
"C:\Dev\Python27\lib\site-packages\django-1.3.4-py2.7.egg\django\db\models\ query.py",
line 84, in __len__
    self._result_cache.extend(self._iter)
  File
"C:\Dev\Python27\lib\site-packages\django-1.3.4-py2.7.egg\django\db\models\ query.py",
line 273, in iterator
    for row in compiler.results_iter():
  File
"C:\Dev\Python27\lib\site-packages\django-1.3.4-py2.7.egg\django\db\models\ sql\compiler.py",
line 680, in results
_iter
    for rows in self.execute_sql(MULTI):
  File
"C:\Dev\Python27\lib\site-packages\django-1.3.4-py2.7.egg\django\db\models\ sql\compiler.py",
line 735, in execute
_sql
    cursor.execute(sql, params)
  File
"C:\Dev\Python27\lib\site-packages\django-1.3.4-py2.7.egg\django\db\backend s\util.py",
line 34, in execute
    return self.cursor.execute(sql, params)
  File
"C:\Dev\Python27\lib\site-packages\django-1.3.4-py2.7.egg\django\db\backend s\postgresql_psycopg2\base.py",
line 4
4, in execute
    return self.cursor.execute(query, args)
DatabaseError: syntax error at or near ")"
LINE 1: ...ugtest_book" WHERE "bugtest_book"."author_id" IN () LIMIT 21
                                                             ^
>>> import django; django.VERSION

(1, 3, 4, 'final', 0)

It's also database dependent, I couldn't reproduce the issue with SQLite.

I've created a ticket, https://code.djangoproject.com/ticket/19263, but
I've no idea how to classify it.

Thanks in advance for looking at this,

Chi Ho Kwok


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »