From: Mike Bayer Date: Sat, 21 Mar 2015 17:12:15 +0000 (-0400) Subject: - Fixed bug in new "label resolution" feature of :ticket:`2992` where X-Git-Tag: rel_1_0_0b4~12 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=c09c21c9f31826e126b97d6318a9df66986af774;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Fixed bug in new "label resolution" feature of :ticket:`2992` where the string label placed in the order_by() or group_by() of a statement would place higher priority on the name as found inside the FROM clause instead of a more locally available name inside the columns clause. fixes #3335 --- diff --git a/doc/build/changelog/changelog_10.rst b/doc/build/changelog/changelog_10.rst index eb68b43a4b..9f68d05eb1 100644 --- a/doc/build/changelog/changelog_10.rst +++ b/doc/build/changelog/changelog_10.rst @@ -15,6 +15,19 @@ .. include:: changelog_07.rst :start-line: 5 +.. changelog:: + :version: 1.0.0b4 + + .. change:: + :tags: bug, sql + :tickets: 3335 + + Fixed bug in new "label resolution" feature of :ticket:`2992` where + the string label placed in the order_by() or group_by() of a statement + would place higher priority on the name as found + inside the FROM clause instead of a more locally available name + inside the columns clause. + .. changelog:: :version: 1.0.0b3 :released: March 20, 2015 diff --git a/lib/sqlalchemy/__init__.py b/lib/sqlalchemy/__init__.py index 94ab772b14..c1e84733c3 100644 --- a/lib/sqlalchemy/__init__.py +++ b/lib/sqlalchemy/__init__.py @@ -120,7 +120,7 @@ from .schema import ( from .inspection import inspect from .engine import create_engine, engine_from_config -__version__ = '1.0.0b3' +__version__ = '1.0.0b4' def __go(lcls): diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py index 6520f08fc8..b5a7489bf9 100644 --- a/lib/sqlalchemy/sql/selectable.py +++ b/lib/sqlalchemy/sql/selectable.py @@ -2680,7 +2680,8 @@ class Select(HasPrefixes, HasSuffixes, GenerativeSelect): only_froms = dict( (c.key, c) for c in _select_iterables(self.froms) if c._allow_label_resolve) - with_cols.update(only_froms) + for key, value in only_froms.items(): + with_cols.setdefault(key, value) return with_cols, only_froms diff --git a/test/sql/test_text.py b/test/sql/test_text.py index c2f4d2c151..4483597ac0 100644 --- a/test/sql/test_text.py +++ b/test/sql/test_text.py @@ -574,6 +574,20 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): "FROM mytable AS mytable_1 ORDER BY mytable_1.name" ) + def test_order_by_outermost_label(self): + # test [ticket:3335], assure that order_by("foo") + # catches the label named "foo" in the columns clause only, + # and not the label named "foo" in the FROM clause + s1 = select([table1.c.myid.label("foo"), table1.c.name]).alias() + stmt = select([s1.c.name, func.bar().label("foo")]).order_by("foo") + + self.assert_compile( + stmt, + "SELECT anon_1.name, bar() AS foo FROM " + "(SELECT mytable.myid AS foo, mytable.name AS name " + "FROM mytable) AS anon_1 ORDER BY foo" + ) + def test_unresolvable_warning_order_by(self): stmt = select([table1.c.myid]).order_by('foobar') self._test_warning(