Opened 10 years ago

Closed 4 years ago

Last modified 4 years ago

#640 closed defect (fixed)

order_with_respect_to option fails

Reported by: Bless Owned by: nobody
Component: Documentation Version: master
Severity: major Keywords: order_with_respect_to ordering mysql bug
Cc: starplant@…, gary.wilson@…, fcurella Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Example model code:

from django.core import meta

class Country(meta.Model):
    domain = meta.CharField(maxlength=2, unique=True)
    name = meta.CharField(maxlength=50, unique=True)
    
    class META:
        module_name = verbose_name_plural = "countries"
        ordering = ['domain']
        admin = meta.Admin()
    
    def __repr__(self):
        return self.domain

class State(meta.Model):
    name = meta.CharField(maxlength=100)
    country = meta.ForeignKey(Country)
    
    class META:
        order_with_respect_to = 'country'
        #ordering = ['name']
        admin = meta.Admin()
    
    def __repr__(self):
        return self.name

In Site administration, if fails when i click in States table and Change (States) too
but it fails when i add 'order_with_respect_to' option only

There's been an error:

Traceback (most recent call last):

  File "/usr/lib/python2.4/site-packages/django/core/handlers/base.py", line 77, in get_response
    raise e

FieldDoesNotExist: name=_order

rev. 917 - Python 2.4.2 - sqlite-3.2.1 - pysqlite-2.0.4

Attachments (2)

640.diff (466 bytes) - added by fcurella 4 years ago.
Added a note to the doc
640_patch.diff (658 bytes) - added by poirier 4 years ago.
Add a note to the order_with_respect_to doc

Download all attachments as: .zip

Change History (27)

comment:1 Changed 9 years ago by anonymous

  • Component changed from Admin interface to Database wrapper
  • milestone set to Version 0.93
  • priority changed from normal to highest
  • Severity changed from normal to minor
  • Type changed from defect to task

comment:2 Changed 9 years ago by Gary Wilson <gary.wilson@…>

  • milestone changed from Version 0.93 to Version 1.0

0.93 has come and gone.

comment:3 Changed 9 years ago by anonymous

I think this might be the same bug as http://code.djangoproject.com/ticket/1760. If it is, there is a fix in that bug already.

comment:4 Changed 9 years ago by Gary Wilson <gary.wilson@…>

  • Cc gary.wilson@… added

comment:5 Changed 9 years ago by david@…

  • Keywords order_with_respect_to ordering mysql bug added
  • Type changed from task to defect
  • Version set to SVN

It seems this problem still exists post magic-removal. Since I am seeing very similar behavior, I thought I'd include an updated code example that demonstrates the issue in the latest version of django.

I am using MySQL 4.1, python 2.4.4, and django (svn rev 4067)

The following is my model code:

from django.db import models

# Create your models here.
# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
#     * Rearrange models' order
#     * Make sure each model has one field with primary_key=True
# Feel free to rename the models, but don't rename db_table values or field names.
#
# Also note: You'll have to insert the output of 'django-admin.py sqlinitialdata [appname]'
# into your database.

from django.db import models
import crypt

ACCOUNT_OPTION_CHOICES = (('vacation_message', 'Vacation Autoreply'),)


class Domain(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(maxlength=64, null=False)
    alias_for = models.ForeignKey('self', null=True, db_column='alias_for', blank=True)
    active = models.BooleanField()
    class Meta:
        db_table = 'domain'
        ordering = ['name']
    class Admin:
        list_display = ('name', 'active')

    def __str__(self):
        return '%s' % (self.name,)

class Account(models.Model):
    id = models.AutoField(primary_key=True)
    username = models.CharField(maxlength=128, core=True)
    uid = models.IntegerField(null=True, blank=True, editable=False)
    gid = models.IntegerField(null=True, blank=True, editable=False)
    crypt_password = models.CharField(maxlength=128, editable=False)
    password = models.CharField(blank=True, maxlength=128)
    quota = models.CharField(maxlength=255, editable=False)
    active = models.BooleanField()
    maildir = models.CharField(maxlength=255, editable=False)
    domain = models.ForeignKey(Domain, null=False)
    class Meta:
        db_table = 'account'
#        ordering = ['username']
        order_with_respect_to = 'domain'
    class Admin:
        list_display = ('username', 'domain', 'active')

#    def save(self):
#        self.crypt_password = crypt.crypt(self.password, self.username)
#        if self.maildir == None or len(self.maildir) < 1:
#            self.maildir = '/var/mail/%s/%s/' % (self.domain, self.username)
#        super(Account, self).save()

    def __str__(self):
        return '%s@%s' % (self.username,self.domain)

Below is the SQL for the tables in my database that correspond to the above models:

CREATE TABLE `domain` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(64) NOT NULL default '',
  `alias_for` int(11) default NULL,
  `active` tinyint(1) NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `alias_for` (`alias_for`),
  CONSTRAINT `domain_ibfk_1` FOREIGN KEY (`alias_for`) REFERENCES `domain` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1

CREATE TABLE `account` (
  `id` int(11) NOT NULL auto_increment,
  `username` varchar(128) NOT NULL default '',
  `uid` int(11) default NULL,
  `gid` int(11) default NULL,
  `crypt_password` varchar(128) NOT NULL default '',
  `password` varchar(128) default NULL,
  `quota` varchar(255) NOT NULL default '5M',
  `active` tinyint(1) NOT NULL default '1',
  `maildir` varchar(255) NOT NULL default '',
  `domain_id` int(11) default NULL,
  PRIMARY KEY  (`id`),
  KEY `domain_id` (`domain_id`),
  CONSTRAINT `account_ibfk_1` FOREIGN KEY (`domain_id`) REFERENCES `domain` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1

This is the error I get on .save() of an Account instance:

  File "setup_accounts.py", line 32, in ?
    a.save()
  File "/usr/lib/python2.4/site-packages/django/db/models/base.py", line 204, in save
    ','.join(placeholders)), db_values)
  File "/usr/lib/python2.4/site-packages/django/db/backends/util.py", line 12, in execute
    return self.cursor.execute(sql, params)
  File "/usr/lib/python2.4/site-packages/django/db/backends/mysql/base.py", line 42, in execute
    return self.cursor.execute(sql, params)
  File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 163, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line 35, in defaulterrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.OperationalError: (1054, "Unknown column '_order' in 'field list'")

This is the error I get on attempting to view an Accounts in the django admin site:

Traceback (most recent call last):
File "/usr/lib/python2.4/site-packages/django/template/__init__.py" in render_node
  712. result = node.render(context)
File "/usr/lib/python2.4/site-packages/django/template/__init__.py" in render
  868. dict = func(*args)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/templatetags/admin_list.py" in result_list
  182. return {'cl': cl,
File "/usr/lib/python2.4/site-packages/django/contrib/admin/templatetags/admin_list.py" in results
  178. for res in cl.result_list:
File "/usr/lib/python2.4/site-packages/django/db/models/query.py" in __iter__
  103. return iter(self._get_data())
File "/usr/lib/python2.4/site-packages/django/db/models/query.py" in _get_data
  430. self._result_cache = list(self.iterator())
File "/usr/lib/python2.4/site-packages/django/db/models/query.py" in iterator
  172. 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)
File "/usr/lib/python2.4/site-packages/django/db/backends/mysql/base.py" in execute
  42. return self.cursor.execute(sql, params)
File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py" in execute
  163. self.errorhandler(self, exc, value)
File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py" in defaulterrorhandler
  35. raise errorclass, errorvalue

  OperationalError at /admin/manager/account/
  (1054, "Unknown column 'account._order' in 'order clause'")

So, just FYI, it seems that this is still something that needs fixing.

comment:6 Changed 9 years ago by Treeform

While you are at it you can also look at other order_with_respect_to ...

custom ordered fields are a must for many sites! World is not that simple when you cant sort it by date.

The other tickets on this subject that do not receive attention:
http://code.djangoproject.com/ticket/2740
http://code.djangoproject.com/ticket/2137

There needs to be some though on sortable relation fields.

comment:7 Changed 9 years ago by anonymous

  • Cc starplant@… added
  • Severity changed from minor to major

comment:8 Changed 9 years ago by anonymous

  • milestone Version 1.0 deleted

Milestone Version 1.0 deleted

comment:9 Changed 9 years ago by Gary Wilson <gary.wilson@…>

  • Triage Stage changed from Unreviewed to Accepted

comment:10 Changed 8 years ago by anonymous

  • Triage Stage changed from Accepted to Ready for checkin
  • Version changed from SVN to new-admin

comment:11 Changed 8 years ago by anonymous

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

comment:12 Changed 8 years ago by anonymous

  • Needs documentation set

comment:13 Changed 8 years ago by anonymous

  • Component changed from Database wrapper to Cache system
  • Has patch set
  • Needs tests set
  • Patch needs improvement set
  • Triage Stage changed from Ready for checkin to Accepted
  • Version changed from new-admin to SVN

testing

comment:14 Changed 8 years ago by mir@…

  • Component changed from Cache system to Database wrapper
  • Has patch unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution fixed deleted
  • Status changed from closed to reopened

Reverted anonymous actions.

comment:15 Changed 8 years ago by anonymous

  • Resolution set to fixed
  • Status changed from reopened to closed

comment:16 Changed 8 years ago by SmileyChris

  • Resolution fixed deleted
  • Status changed from closed to reopened

Reverting anonymous close

comment:17 Changed 8 years ago by ubernostrum

  • Resolution set to invalid
  • Status changed from reopened to closed

I have a very strong suspicion that the issues described here are a symptom of adding an order_with_respect_to directive once the table was already created, in which case nothing in Django will automatically add the necessary extra column for ordering.

comment:18 Changed 4 years ago by Hraban

  • Component changed from Database layer (models, ORM) to Documentation
  • Easy pickings unset
  • Resolution invalid deleted
  • Status changed from closed to reopened
  • UI/UX unset

And 4 years later, the report and ubernostrum's comment are still valid:
We humble users experience "bugs", because order_with_respect_to behaviour isn't documented.
I.e. the docs (https://docs.djangoproject.com/en/1.3/ref/models/options/#order-with-respect-to) should clearly state that this option adds an additional field "_order" to the model and thus can only work at model creation time (or proper model migration, that is).

comment:19 Changed 4 years ago by fcurella

  • Easy pickings set

Changed 4 years ago by fcurella

Added a note to the doc

comment:20 Changed 4 years ago by fcurella

  • Needs documentation unset

comment:21 Changed 4 years ago by fcurella

  • Cc fcurella added

Changed 4 years ago by poirier

Add a note to the order_with_respect_to doc

comment:22 Changed 4 years ago by poirier

  • Has patch set

I suggest saying a little bit more - the patch I just attached says

order_with_respect_to requires an additional database column,
so be sure to take that into account if you add or change
order_with_respect_to after your initial syncdb, as you
would any other change to your models.

comment:23 Changed 4 years ago by claudep

  • Triage Stage changed from Accepted to Ready for checkin

comment:24 Changed 4 years ago by timo

  • Resolution set to fixed
  • Status changed from reopened to closed

In [17316]:

Fixed #640 - Documented that changing order_with_respect_to requires a schema change; thanks fcurella and poirier for the draft patches.

comment:25 Changed 4 years ago by timo

In [17317]:

[1.3.X] Fixed #640 - Documented that changing order_with_respect_to requires a schema change; thanks fcurella and poirier for the draft patches.

Backport of r17316 from trunk.

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