Code

Opened 8 years ago

Closed 8 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
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: UI/UX:

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 (0)

Change History (4)

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

  • Component changed from Admin interface to Database wrapper

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

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

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

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

comment:4 Changed 8 years ago by jkocherhans

  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #1245

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.