Opened 18 years ago

Closed 18 years ago

#1547 closed defect (duplicate)

Wrong column name in SQL ORDER BY statement

Reported by: Nebojsa Djordjevic <nesh at studioquattro dot co dot yu> Owned by: Adrian Holovaty
Component: Database layer (models, ORM) Version: magic-removal
Severity: normal Keywords:
Cc: nesh@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When I have models like this:

class Foo(models.Model):
   a = models.SlugField('a')
   class Admin:
     pass

class Bar(models.Model):
   a = models.SlugField('a')
   class Admin:
     pass

class Baz(models.Model):
   parent = models.OneToOneField(Foo)
   bar = models.ForeignKey(Bar)
   class Admin:
     pass

Any attempt to access Baz model thru admin interface fails with OperationalError at <address> no such column: <app>_foo.id and generated SQL looks like this:

SELECT "<app>_baz"."parent_id","<app>_baz"."bar_id" FROM "<app>_baz" ORDER BY "<app>_foo"."id" ASC

I think that correct SQL is more like this:

SELECT "<app>_baz"."parent_id","<app>_baz"."bar_id" FROM "<app>_baz" ORDER BY "<app>_baz"."foo_id" ASC

Current workaround is that I must declare model like this:

class Baz(models.Model):
   parent = models.OneToOneField(Foo)
   bar = models.ForeignKey(Bar)
   class Meta:
       ordering = ['?']
   class Admin:
     pass

Note ? for forcing no ordering.

If I have time I'll lookup into source to find what's going on (and hopefully submit a patch), or I'm simply doing something wrong here?

Change History (4)

comment:1 by Nebojsa Djordjevic <nesh at studioquattro dot co dot yu>, 18 years ago

Component: Admin interfaceDatabase wrapper

comment:2 by Nebojsa Djordjevic <nesh at studioquattro dot co dot yu>, 18 years ago

I forgot to mention that I'm using SQLite database.

comment:3 by Nebojsa Djordjevic <nesh at studioquattro dot co dot yu>, 18 years ago

Also a ordering = ['parent_id'] works (with validate complaining about missing column ;) )

comment:4 by jkocherhans, 18 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #1245

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