On Friday 06 June 2008 17:09:02 Michael Bayer wrote:
> On Jun 6, 2008, at 3:27 AM, [EMAIL PROTECTED] wrote:
> > i have a premade query with some order_by but i want to replace
> > the order_by. any "legal" way to do it?
> > q=q.order_by(a,b,c) seems to only add a,b,c to existing order_by;
> > q=q.order_by() and  q=q.order_by(None) do nothing
>
> order_by(None) should do it.   can you take a look at that for me ?
> (0.4 or 0.5 ?)
0.4/0.5 behave same - they have ~same code in orm.query.py:

    def order_by(self, *criterion):
        criterion = [ self._adapt_clause(
                expression._literal_as_text(o), True, True) 
                for o in criterion]

        if self._order_by is False:
            self._order_by = criterion
        else:
            self._order_by = self._order_by + criterion

    order_by = util.array_as_starargs_decorator( order_by)
    order_by = _generative( 
                        __no_statement_condition, 
                        __no_limit_offset
                )( order_by)

this will only add columns.
here a test case:

from sqlalchemy import *
from sqlalchemy.orm import *
m = MetaData('sqlite:///')
m.bind.echo=True
t = Table( 't', m,
        Column('a', Integer),
        Column('b', Integer, primary_key=True)
    )
class T(object): pass
mapper( T,t)
m.create_all()
s = create_session()
q = s.query(T).order_by( T.a).order_by( None).order_by( T.b)
print q.all()

the result query is:
SELECT t.a AS t_a, t.b AS t_b 
FROM t ORDER BY t.a, t.b

adding something like this at beginning of Query.order_by() would fix 
it:
       if not criterion:
            self._order_by = False
            return

now the result query is what expected:
SELECT t.a AS t_a, t.b AS t_b 
FROM t ORDER BY t.b

patches attached.
ciao
svilen

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Index: orm/query.py
===================================================================
--- orm/query.py	(revision 4836)
+++ orm/query.py	(working copy)
@@ -570,6 +570,10 @@

         q = self.__no_statement("order_by")

+        if not criterion:       #cancel any order_by
+            q._order_by = False
+            return q
+
         if self._aliases_tail:
             criterion = tuple(self._aliases_tail.adapt_list(
                     [expression._literal_as_text(o) for o in criterion]
Index: orm/query.py
===================================================================
--- orm/query.py	(revision 4836)
+++ orm/query.py	(working copy)
@@ -661,6 +661,10 @@
     def order_by(self, *criterion):
         """apply one or more ORDER BY criterion to the query and return the newly resulting ``Query``"""

+        if not criterion:       #cancel any order_by
+            self._order_by = False
+            return
+
         criterion = [self._adapt_clause(expression._literal_as_text(o), True, True) for o in criterion]

         if self._order_by is False:

Reply via email to