Opened 12 years ago

Closed 11 years ago

Last modified 11 years ago

#2247 closed defect (duplicate)

Foreign key ordering bug

Reported by: timosa2@… Owned by: Adrian Holovaty
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:


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:
    class Meta:
        ordering = ['date', 'description']

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

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

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

The error:

Traceback (most recent call last):
File "/usr/lib/python2.4/site-packages/django/core/handlers/" in get_response
  74. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/views/" in _checklogin
  54. return view_func(request, *args, **kwargs)
File "/usr/lib/python2.4/site-packages/django/views/decorators/" in _wrapped_view_func
  40. response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/views/" in change_list
  739. cl = ChangeList(request, model)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/views/" in __init__
  570. self.get_results(request)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/views/" in get_results
  628. result_list = list(self.query_set)
File "/usr/lib/python2.4/site-packages/django/db/models/" in __iter__
  94. return iter(self._get_data())
File "/usr/lib/python2.4/site-packages/django/db/models/" in _get_data
  412. self._result_cache = list(self.iterator())
File "/usr/lib/python2.4/site-packages/django/db/models/" 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/" 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

Change History (5)

comment:1 Changed 12 years ago by Adrian Holovaty

Status: newassigned

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

comment:2 Changed 12 years ago by Adrian Holovaty

Resolution: worksforme
Status: assignedclosed

I cannot reproduce this error.

comment:3 Changed 11 years ago by adament@…

Resolution: worksforme
Status: closedreopened

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 11 years ago by Adam Ehlers Nyholm Thomsen <adament@…>

Resolution: duplicate
Status: reopenedclosed

This looks like a duplicate of #2247

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

Sorry #2076

Note: See TracTickets for help on using tickets.
Back to Top