From: Mike Bayer Date: Fri, 16 Nov 2018 00:48:45 +0000 (-0500) Subject: Repair tutorial code example to be deterministic X-Git-Tag: rel_1_3_0b1~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=477a64e2b26a84cbf6d91f0ee292c7adc50d7a88;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Repair tutorial code example to be deterministic A few queries featured an aggregate without a GROUP BY or a complete ordering, causing the doctests to fail with recent versions of SQLite. The queries are now made to include a GROUP BY as well as ORDER BY both columns. Fixes: #4370 Change-Id: I904bddb4372224158fcecff9f0dbbbe7c1bf36b3 --- diff --git a/doc/build/core/tutorial.rst b/doc/build/core/tutorial.rst index baddfc459e..a991532273 100644 --- a/doc/build/core/tutorial.rst +++ b/doc/build/core/tutorial.rst @@ -1024,7 +1024,8 @@ fields within an "OVER" or "DISTINCT" clause. If we have such a label in our :func:`.select` construct, we can refer to it directly by passing the string straight into :meth:`.select.order_by` or :meth:`.select.group_by`, among others. This will refer to the named label and also prevent the -expression from being rendered twice: +expression from being rendered twice. Label names that resolve to columns +are rendered fully: .. sourcecode:: pycon+sql @@ -1032,13 +1033,13 @@ expression from being rendered twice: >>> stmt = select([ ... addresses.c.user_id, ... func.count(addresses.c.id).label('num_addresses')]).\ - ... order_by("num_addresses") + ... group_by("user_id").order_by("user_id", "num_addresses") {sql}>>> conn.execute(stmt).fetchall() SELECT addresses.user_id, count(addresses.id) AS num_addresses - FROM addresses ORDER BY num_addresses + FROM addresses GROUP BY addresses.user_id ORDER BY addresses.user_id, num_addresses () - {stop}[(2, 4)] + {stop}[(1, 2), (2, 2)] We can use modifiers like :func:`.asc` or :func:`.desc` by passing the string name: @@ -1049,13 +1050,13 @@ name: >>> stmt = select([ ... addresses.c.user_id, ... func.count(addresses.c.id).label('num_addresses')]).\ - ... order_by(desc("num_addresses")) + ... group_by("user_id").order_by("user_id", desc("num_addresses")) {sql}>>> conn.execute(stmt).fetchall() SELECT addresses.user_id, count(addresses.id) AS num_addresses - FROM addresses ORDER BY num_addresses DESC + FROM addresses GROUP BY addresses.user_id ORDER BY addresses.user_id, num_addresses DESC () - {stop}[(2, 4)] + {stop}[(1, 2), (2, 2)] Note that the string feature here is very much tailored to when we have already used the :meth:`~.ColumnElement.label` method to create a