Now I think I have a problem with "DESC" (see input and output at end
of message):
orderBy=so.DESC(Country.q.code) gives correct results, while
order=-Country.q.code does not ... but I agree using "DESC" seems
cleaner.
orderBy=Country.q.name and orderBy=-Country.q.name both work and give
correct results, but orderBy=so.DESC(Country.q.name) raises exception.
So how can I safely build an orderBy clause?
------------------------------------------------------------------------------
import sys
sys.path.insert(0, 'lib/FormEncode-0.4-py2.4.egg')
sys.path.insert(0, 'lib/SQLObject-0.7.0-py2.4.egg')
import sqlobject as so
import sqlobject.inheritance
so.sqlhub.processConnection = so.connectionForURI('sqlite:/:memory:?debug=1')
class Entity(so.inheritance.InheritableSQLObject):
#name = so.UnicodeCol(notNone=1)
pass
class Country(Entity):
code = so.StringCol(length=2) # iso3166-1 Alpha-2
Entity.sqlmeta.addColumn(so.UnicodeCol('name', notNone=1))
Country.q.name = Entity.q.name
Entity.createTable(ifNotExists=True)
Country.createTable(ifNotExists=True)
Country(name='Switzerland', code='CH')
Country(name='South Africa', code='ZA')
print 1, 1*"-"
print list(Country.select(orderBy=Country.q.code))
print 2, 2*"-"
print list(Country.select(orderBy=-Country.q.code))
print 3, 3*"-"
print list(Country.select(orderBy=so.DESC(Country.q.code)))
print 4, 4*"-"
print list(Country.select(orderBy=Country.q.name))
print 5, 5*"-"
print list(Country.select(orderBy=-Country.q.name))
print 6, 6*"-"
print list(Country.select(orderBy=so.DESC(Country.q.name)))
------------------------------------------------------------------------------
1 -
1/Select : SELECT country.id, country.child_name, country.code FROM
country WHERE 1 = 1 ORDER BY country.code
1/QueryR : SELECT country.id, country.child_name, country.code FROM
country WHERE 1 = 1 ORDER BY country.code
1/COMMIT : auto
[<Country 1 code='CH' name=u'Switzerland'>, <Country 2 code='ZA'
name=u'South Africa'>]
2 --
1/Select : SELECT country.id, country.child_name, country.code FROM
country WHERE 1 = 1 ORDER BY - country.code
1/QueryR : SELECT country.id, country.child_name, country.code FROM
country WHERE 1 = 1 ORDER BY - country.code
1/COMMIT : auto
[<Country 1 code='CH' name=u'Switzerland'>, <Country 2 code='ZA'
name=u'South Africa'>]
3 ---
1/Select : SELECT country.id, country.child_name, country.code FROM
country WHERE 1 = 1 ORDER BY country.code DESC
1/QueryR : SELECT country.id, country.child_name, country.code FROM
country WHERE 1 = 1 ORDER BY country.code DESC
1/COMMIT : auto
[<Country 2 code='ZA' name=u'South Africa'>, <Country 1 code='CH'
name=u'Switzerland'>]
4 ----
1/Select : SELECT country.id, country.child_name, country.code FROM
country, entity WHERE (1 = 1 AND (country.id = entity.id)) ORDER BY
entity.name
1/QueryR : SELECT country.id, country.child_name, country.code FROM
country, entity WHERE (1 = 1 AND (country.id = entity.id)) ORDER BY
entity.name
1/COMMIT : auto
[<Country 2 code='ZA' name=u'South Africa'>, <Country 1 code='CH'
name=u'Switzerland'>]
5 -----
1/Select : SELECT country.id, country.child_name, country.code FROM
country, entity WHERE (1 = 1 AND (country.id = entity.id)) ORDER BY -
entity.name
1/QueryR : SELECT country.id, country.child_name, country.code FROM
country, entity WHERE (1 = 1 AND (country.id = entity.id)) ORDER BY -
entity.name
1/COMMIT : auto
[<Country 1 code='CH' name=u'Switzerland'>, <Country 2 code='ZA'
name=u'South Africa'>]
6 ------
1/Select : SELECT country.id, country.child_name, country.code FROM
country WHERE 1 = 1 ORDER BY entity.name DESC
1/QueryR : SELECT country.id, country.child_name, country.code FROM
country WHERE 1 = 1 ORDER BY entity.name DESC
Traceback (most recent call last):
File "inh.py", line 34, in ?
print list(Country.select(orderBy=so.DESC(Country.q.name)))
File "lib\SQLObject-0.7.0-py2.4.egg\sqlobject\sresults.py", line
149, in __iter__
File "lib\SQLObject-0.7.0-py2.4.egg\sqlobject\sresults.py", line
157, in lazyIter
File "lib\SQLObject-0.7.0-py2.4.egg\sqlobject\dbconnection.py", line
361, in iterSelect
File "lib\SQLObject-0.7.0-py2.4.egg\sqlobject\inheritance\iteration.py",
line 10, in __init__
File "lib\SQLObject-0.7.0-py2.4.egg\sqlobject\dbconnection.py", line
687, in __init__
File "lib\SQLObject-0.7.0-py2.4.egg\sqlobject\dbconnection.py", line
295, in _executeRetry
pysqlite2.dbapi2.OperationalError: no such column: entity.name
1/COMMIT : auto
------------------------------------------------------------------------------
--
jt