]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Repair tutorial code example to be deterministic
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 16 Nov 2018 00:48:45 +0000 (19:48 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 16 Nov 2018 00:48:45 +0000 (19:48 -0500)
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

doc/build/core/tutorial.rst

index baddfc459e292101299f2bd056bae3db68c3cb56..a991532273c39cc8b2524f9463c0449e6d6f194e 100644 (file)
@@ -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