Opened 19 years ago
Closed 19 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 , 19 years ago
Component: | Admin interface → Database wrapper |
---|
comment:2 by , 19 years ago
comment:3 by , 19 years ago
Also a ordering = ['parent_id']
works (with validate complaining about missing column ;) )
I forgot to mention that I'm using SQLite database.