From: Mike Bayer Date: Thu, 11 Mar 2010 23:04:57 +0000 (-0500) Subject: - Calling query.order_by() or query.distinct() before calling X-Git-Tag: rel_0_6beta2~57^2~8 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=409c8ed703c7391380164c5d265c8401e65060c4;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Calling query.order_by() or query.distinct() before calling query.select_from(), query.with_polymorphic(), or query.from_statement() raises an exception now instead of silently dropping those criterion. [ticket:1736] --- diff --git a/CHANGES b/CHANGES index 00cb3384e3..101c0223bd 100644 --- a/CHANGES +++ b/CHANGES @@ -103,6 +103,11 @@ CHANGES it also is implemented within merge() such that a SELECT won't be issued for an incoming instance with partially NULL primary key if the flag is False. [ticket:1680] + + - Calling query.order_by() or query.distinct() before calling + query.select_from(), query.with_polymorphic(), or + query.from_statement() raises an exception now instead of + silently dropping those criterion. [ticket:1736] - sql - The most common result processors conversion function were diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index 0f68e0a41d..88945b9d07 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -281,12 +281,16 @@ class Query(object): equivs.update(ent.mapper._equivalent_columns) return equivs + def _get_condition(self): + self._order_by = self._distinct = False + return self._no_criterion_condition("get") + def _no_criterion_condition(self, meth): if not self._enable_assertions: return if self._criterion is not None or self._statement is not None or self._from_obj or \ self._limit is not None or self._offset is not None or \ - self._group_by: + self._group_by or self._order_by or self._distinct: raise sa_exc.InvalidRequestError( "Query.%s() being called on a " "Query with existing criterion. " % meth) @@ -1554,7 +1558,7 @@ class Query(object): if refresh_state is None: q = self._clone() - q._no_criterion_condition("get") + q._get_condition() else: q = self._clone() diff --git a/test/orm/test_query.py b/test/orm/test_query.py index 1bc24a2eed..054d64d204 100644 --- a/test/orm/test_query.py +++ b/test/orm/test_query.py @@ -283,6 +283,25 @@ class InvalidGenerationsTest(QueryTest): s = create_session() q = s.query(User) assert_raises(sa_exc.InvalidRequestError, q.add_column, object()) + + def test_distinct(self): + """test that a distinct() call is not valid before 'clauseelement' conditions.""" + + s = create_session() + q = s.query(User).distinct() + assert_raises(sa_exc.InvalidRequestError, q.select_from, User) + assert_raises(sa_exc.InvalidRequestError, q.from_statement, text("select * from table")) + assert_raises(sa_exc.InvalidRequestError, q.with_polymorphic, User) + + def test_order_by(self): + """test that an order_by() call is not valid before 'clauseelement' conditions.""" + + s = create_session() + q = s.query(User).order_by(User.id) + assert_raises(sa_exc.InvalidRequestError, q.select_from, User) + assert_raises(sa_exc.InvalidRequestError, q.from_statement, text("select * from table")) + assert_raises(sa_exc.InvalidRequestError, q.with_polymorphic, User) + def test_mapper_zero(self): s = create_session()