Code

Opened 5 years ago

Closed 5 years ago

Last modified 14 months ago

#10064 closed (fixed)

error: annotate doesn't support select_related

Reported by: olivius Owned by:
Component: Database layer (models, ORM) Version: master
Severity: Keywords: annotate
Cc: flosch@…, elsdoerfer@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by Alex)

models.py:

class Account(models.Model):
    accountname = models.CharField(max_length=20)
    account_creator = models.ForeignKey(User)
    description = models.TextField(_('description'), blank=True)
    
    def __unicode__(self):
        return self.account


class Category(models.Model):
    category_name = models.CharField(max_length=50)
    category_creator = models.ForeignKey(User)
    description = models.TextField(_('description'), blank=True)
    def __unicode__(self):
        return self.category


class Transaction(models.Model):
    category            = models.ForeignKey(Category)
    account_from        = models.ForeignKey(Account, related_name='account_from')
    account_to          = models.ForeignKey(Account, related_name='account_to')
    creator             = models.ForeignKey(User)
    label               = models.CharField(_('label'), max_length=30)
    description         = models.TextField(_('description'), blank=True)
    ammount             = models.DecimalField(_('amount'),max_digits=8, decimal_places=2)
    created_at          = models.DateTimeField(_('created at'), default=datetime.now)
    updated_at          = models.DateTimeField(_('updated at'))
    
    def __unicode__(self):
        return self.label

shell:

>>> transactions=Transaction.objects.select_related('category','account_from','account_to').annotate(Sum('amount')).order_by('category','updated_at')
>>> print transactions
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python25\lib\site-packages\django\db\models\query.py", line 239, in __getitem__
    return list(qs)[0]
  File "C:\Python25\lib\site-packages\django\db\models\query.py", line 163, in __len__
    self._result_cache.extend(list(self._iter))
  File "C:\Python25\lib\site-packages\django\db\models\query.py", line 294, in iterator
    setattr(obj, aggregate, row[i+aggregate_start])
IndexError: tuple index out of range

Attachments (0)

Change History (10)

comment:1 Changed 5 years ago by olivius <oliver.janssen@…>

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 5 years ago by kmtracey

  • Description modified (diff)

(Fixed formatting.)

comment:3 Changed 5 years ago by anonymous

  • Cc flosch@… added

comment:4 Changed 5 years ago by Alex

  • Description modified (diff)

cleaned up the formatting of the description slightly.

comment:5 Changed 5 years ago by olivius <oliver.janssen@…>

models.py:

class Account(models.Model):
    accountname = models.CharField(max_length=20)
    account_creator = models.ForeignKey(User)
    description = models.TextField(_('description'), blank=True)
    
    def __unicode__(self):
        return self.account


class Category(models.Model):
    category_name = models.CharField(max_length=50)
    category_creator = models.ForeignKey(User)
    description = models.TextField(_('description'), blank=True)
    def __unicode__(self):
        return self.category


class Transaction(models.Model):
    category            = models.ForeignKey(Category)
    account_from        = models.ForeignKey(Account, related_name='account_from')
    account_to          = models.ForeignKey(Account, related_name='account_to')
    creator             = models.ForeignKey(User)
    label               = models.CharField(_('label'), max_length=30)
    description         = models.TextField(_('description'), blank=True)
    amount              = models.DecimalField(_('amount'),max_digits=8, decimal_places=2)
    created_at          = models.DateTimeField(_('created at'), default=datetime.now)
    updated_at          = models.DateTimeField(_('updated at'))
    
    def __unicode__(self):
        return self.label

shell:

>>> transactions=Transaction.objects.select_related('category','account_from','account_to').annotate(Sum('amount')).order_by('category','updated_at')
>>> print transactions
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python25\lib\site-packages\django\db\models\query.py", line 239, in __getitem__
    return list(qs)[0]
  File "C:\Python25\lib\site-packages\django\db\models\query.py", line 163, in __len__
    self._result_cache.extend(list(self._iter))
  File "C:\Python25\lib\site-packages\django\db\models\query.py", line 294, in iterator
    setattr(obj, aggregate, row[i+aggregate_start])
IndexError: tuple index out of range

comment:6 Changed 5 years ago by olivius

Typo: changed 'ammount' to 'amount' in class Transaction

comment:7 Changed 5 years ago by anonymous

  • Cc elsdoerfer@… added

comment:8 Changed 5 years ago by russellm

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

(In [9781]) Fixed #10064 -- Corrected handling of aggregate queries that also use select_related(). Thanks to olivius for the report.

comment:9 Changed 5 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

comment:10 Changed 14 months ago by akaariai

  • Component changed from ORM aggregation to Database layer (models, ORM)

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.