Opened 18 years ago

Closed 18 years ago

Last modified 18 years ago

#2247 closed defect (duplicate)

Foreign key ordering bug

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

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

Change History (5)

comment:1 by Adrian Holovaty, 18 years ago

Status: newassigned

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

comment:2 by Adrian Holovaty, 18 years ago

Resolution: worksforme
Status: assignedclosed

I cannot reproduce this error.

comment:3 by adament@…, 18 years ago

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

Resolution: duplicate
Status: reopenedclosed

This looks like a duplicate of #2247

comment:5 by Adam Ehlers Nyholm Thomsen <adament@…>, 18 years ago

Sorry #2076

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