#640 closed defect (fixed)
order_with_respect_to option fails
Reported by: | Bless | Owned by: | nobody |
---|---|---|---|
Component: | Documentation | Version: | dev |
Severity: | major | Keywords: | order_with_respect_to ordering mysql bug |
Cc: | starplant@…, gary.wilson@…, Flavio Curella | 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)
Change History (27)
comment:1 by , 18 years ago
Component: | Admin interface → Database wrapper |
---|---|
milestone: | → Version 0.93 |
priority: | normal → highest |
Severity: | normal → minor |
Type: | defect → task |
comment:2 by , 18 years ago
milestone: | Version 0.93 → Version 1.0 |
---|
comment:3 by , 18 years ago
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 by , 18 years ago
Cc: | added |
---|
comment:5 by , 18 years ago
Keywords: | order_with_respect_to ordering mysql bug added |
---|---|
Type: | task → defect |
Version: | → 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 by , 18 years ago
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 by , 18 years ago
Cc: | added |
---|---|
Severity: | minor → major |
comment:9 by , 18 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:10 by , 18 years ago
Triage Stage: | Accepted → Ready for checkin |
---|---|
Version: | SVN → new-admin |
comment:11 by , 18 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:12 by , 18 years ago
Needs documentation: | set |
---|
comment:13 by , 18 years ago
Component: | Database wrapper → Cache system |
---|---|
Has patch: | set |
Needs tests: | set |
Patch needs improvement: | set |
Triage Stage: | Ready for checkin → Accepted |
Version: | new-admin → SVN |
testing
comment:14 by , 18 years ago
Component: | Cache system → Database wrapper |
---|---|
Has patch: | unset |
Needs tests: | unset |
Patch needs improvement: | unset |
Resolution: | fixed |
Status: | closed → reopened |
Reverted anonymous actions.
comment:15 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
comment:17 by , 17 years ago
Resolution: | → invalid |
---|---|
Status: | reopened → 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 by , 13 years ago
Component: | Database layer (models, ORM) → Documentation |
---|---|
Easy pickings: | unset |
Resolution: | invalid |
Status: | closed → 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 by , 13 years ago
Easy pickings: | set |
---|
comment:20 by , 13 years ago
Needs documentation: | unset |
---|
comment:21 by , 13 years ago
Cc: | added |
---|
comment:22 by , 13 years ago
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 by , 13 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
0.93 has come and gone.