Django

Code

Ticket #1547 (closed: duplicate)

Opened 3 years ago

Last modified 2 years ago

Wrong column name in SQL ORDER BY statement

Reported by: Nebojsa Djordjevic <nesh at studioquattro dot co dot yu> Assigned to: adrian
Milestone: Component: Database layer (models, ORM)
Version: magic-removal Keywords:
Cc: nesh@studioquattro.co.yu Triage Stage: Unreviewed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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?

Attachments

Change History

03/28/06 05:50:02 changed by Nebojsa Djordjevic <nesh at studioquattro dot co dot yu>

  • component changed from Admin interface to Database wrapper.

03/28/06 05:51:27 changed by Nebojsa Djordjevic <nesh at studioquattro dot co dot yu>

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

03/28/06 05:53:39 changed by Nebojsa Djordjevic <nesh at studioquattro dot co dot yu>

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

04/21/06 10:45:05 changed by jkocherhans

  • status changed from new to closed.
  • resolution set to duplicate.

Duplicate of #1245


Add/Change #1547 (Wrong column name in SQL ORDER BY statement)




Change Properties
Action