Code

Opened 8 years ago

Closed 7 years ago

Last modified 7 years ago

#2247 closed defect (duplicate)

Foreign key ordering bug

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

Description

The following kind of model where you would like to order objects indirectly using a foreign key results errors like in below.

The model:

class Day(models.Model):
    date        = models.DateField()
    description = models.CharField(maxlength=20)

    class Admin:
        pass
    class Meta:
        ordering = ['date', 'description']

class DayRow(models.Model):
    day = models.ForeignKey(Day)

    class Admin:
        pass
    class Meta:
        ordering = ['day']

class Slot(models.Model):
    day_row = models.ForeignKey(DayRow)
    info    = model.CharField(maxlength=50)
    
    class Admin:
        pass
    class Meta:
        ordering = ['day_row']

The error:

Traceback (most recent call last):
File "/usr/lib/python2.4/site-packages/django/core/handlers/base.py" in get_response
  74. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/views/decorators.py" in _checklogin
  54. return view_func(request, *args, **kwargs)
File "/usr/lib/python2.4/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  40. response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/views/main.py" in change_list
  739. cl = ChangeList(request, model)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/views/main.py" in __init__
  570. self.get_results(request)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/views/main.py" in get_results
  628. result_list = list(self.query_set)
File "/usr/lib/python2.4/site-packages/django/db/models/query.py" in __iter__
  94. return iter(self._get_data())
File "/usr/lib/python2.4/site-packages/django/db/models/query.py" in _get_data
  412. self._result_cache = list(self.iterator())
File "/usr/lib/python2.4/site-packages/django/db/models/query.py" in iterator
  163. cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params)
File "/usr/lib/python2.4/site-packages/django/db/backends/util.py" in execute
  12. return self.cursor.execute(sql, params)

  ProgrammingError at /admin/xyz/dayrow/
  ERROR: missing FROM-clause entry for table "xyz_day" SELECT "xyz_dayrow"."id","xyz_dayrow"."day_id" FROM "xyz_dayrow" ORDER BY "xyz_day"."date" ASC

Attachments (0)

Change History (5)

comment:1 Changed 8 years ago by adrian

  • Status changed from new to assigned

The best way to fix this would be to catch that at the model validator level.

comment:2 Changed 8 years ago by adrian

  • Resolution set to worksforme
  • Status changed from assigned to closed

I cannot reproduce this error.

comment:3 Changed 7 years ago by adament@…

  • Resolution worksforme deleted
  • Status changed from closed to reopened

I can reproduce this bug.

I use Python v2.4.4-2 and PostgreSQL v8.1.7-1 with Psycopg2 v2.0.5.1-6 all of it from Debian Etch.
I use Django SVN r4463.
Might it be a problem with the django psycopg2 database component?

comment:4 Changed 7 years ago by Adam Ehlers Nyholm Thomsen <adament@…>

  • Resolution set to duplicate
  • Status changed from reopened to closed

This looks like a duplicate of #2247

comment:5 Changed 7 years ago by Adam Ehlers Nyholm Thomsen <adament@…>

Sorry #2076

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.