Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
#12615: Broken queryset indexing with postgresql_psycopg2 with custom through model
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
 
Django  
View profile  
 More options Jan 14 2010, 7:13 pm
From: "Django" <nore...@djangoproject.com>
Date: Fri, 15 Jan 2010 00:13:00 -0000
Subject: [Django] #12615: Broken queryset indexing with postgresql_psycopg2 with custom through model
#12615: Broken queryset indexing with postgresql_psycopg2 with custom through model
---------------------------+----------------------------------------------- -
 Reporter:  yxven          |       Owner:  nobody    
   Status:  new            |   Milestone:            
Component:  Uncategorized  |     Version:  1.1-beta-1
 Keywords:                 |       Stage:  Unreviewed
Has_patch:  0              |  
---------------------------+----------------------------------------------- -
 If you start a new project and setup a database using postgresl_psycopg2
 (I know sqlite3 works. I dunno about others), and create a model that uses
 a custom through model, queryset indexing will be broken when accessed
 through that relationship.

 I've attached a simplified models.py and tests.py to explain and
 demonstrate it.

--
Ticket URL: <http://code.djangoproject.com/ticket/12615>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.


 
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.
Django  
View profile  
 More options Jan 15 2010, 3:13 am
From: "Django" <nore...@djangoproject.com>
Date: Fri, 15 Jan 2010 08:13:52 -0000
Local: Fri, Jan 15 2010 3:13 am
Subject: Re: [Django] #12615: Broken queryset indexing with postgresql_psycopg2 with custom through model
#12615: Broken queryset indexing with postgresql_psycopg2 with custom through model
------------------------------------+-------------------------------------- -
          Reporter:  yxven          |         Owner:  nobody    
            Status:  closed         |     Milestone:            
         Component:  Uncategorized  |       Version:  1.1-beta-1
        Resolution:  invalid        |      Keywords:            
             Stage:  Unreviewed     |     Has_patch:  0        
        Needs_docs:  0              |   Needs_tests:  0        
Needs_better_patch:  0              |  
------------------------------------+-------------------------------------- -
Changes (by russellm):

  * status:  new => closed
  * needs_better_patch:  => 0
  * resolution:  => invalid
  * needs_tests:  => 0
  * needs_docs:  => 0

Comment:

 Although confusing, this actually isn't an error on Django's part. Your
 code is making an assumption that isn't valid.

 Your failing test case is essentially the following code:
 {{{
         zipcodes = Zipcode.objects.filter(people = self.bob)
         print zipcodes[0]
         print zipcodes[1]
 }}}

 You are assuming that zipcodes is an ordered list, and that you are
 indexing into that list. You aren't. Zipcodes is an unevaluated queryset.
 The queryset is evaluated twice - once for each subscript.`zipcodes[0]`
 translates to the SQL query:

 {{{
 SELECT * from testapp_zipcode LIMIT 1;
 }}}

 ... and `zipcodes[1]` translates to:
 {{{
 SELECT * from testapp_zipcode LIMIT 1 OFFSET 1;
 }}}

 Zipcodes doesn't have any inherent ordering, so there is no guarantee that
 the database will return zipcode objects in the same order for the two
 queries. As a result, you are seeing id=2 returned for both queries.

 This won't even be 100% reproducible - it really does depend on the
 internals of the database at the time you run the two queries. The
 inconsistency between databases is one example of how this problem can
 manifest.

 There are several ways you can fix your code:
  1. Put an order_by() clause on your query
  2. Put an ordering clause in the Meta definition for the Zipcode model
  3. Fully evaluate the query before indexing into it (i.e.,
 list(zipcode.values()). By forcing the query to be evaluated, the index
 operators will operate on the cached result of the full queryset, rather
 than issuing two individual queries.

--
Ticket URL: <http://code.djangoproject.com/ticket/12615#comment:1>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.


 
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.
Django  
View profile  
 More options Jan 15 2010, 3:16 pm
From: "Django" <nore...@djangoproject.com>
Date: Fri, 15 Jan 2010 20:16:22 -0000
Local: Fri, Jan 15 2010 3:16 pm
Subject: Re: [Django] #12615: Broken queryset indexing with postgresql_psycopg2 with custom through model
#12615: Broken queryset indexing with postgresql_psycopg2 with custom through model
------------------------------------+-------------------------------------- -
          Reporter:  yxven          |         Owner:  nobody    
            Status:  closed         |     Milestone:            
         Component:  Uncategorized  |       Version:  1.1-beta-1
        Resolution:  invalid        |      Keywords:            
             Stage:  Unreviewed     |     Has_patch:  0        
        Needs_docs:  0              |   Needs_tests:  0        
Needs_better_patch:  0              |  
------------------------------------+-------------------------------------- -
Comment (by yxven):

 You are right. Thank you for the correction.

--
Ticket URL: <http://code.djangoproject.com/ticket/12615#comment:2>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.


 
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 »