Browse Source

Fixed #34409 -- Doc'd limitation of dictfetchall() and namedtuplefetchall() examples.

Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>
Jesper Olsson 2 years ago
parent
commit
216eb63883
1 changed files with 11 additions and 2 deletions
  1. 11 2
      docs/topics/db/sql.txt

+ 11 - 2
docs/topics/db/sql.txt

@@ -323,7 +323,10 @@ small performance and memory cost, you can return results as a ``dict`` by
 using something like this::
 
     def dictfetchall(cursor):
-        "Return all rows from a cursor as a dict"
+        """
+        Return all rows from a cursor as a dict.
+        Assume the column names are unique.
+        """
         columns = [col[0] for col in cursor.description]
         return [dict(zip(columns, row)) for row in cursor.fetchall()]
 
@@ -336,11 +339,17 @@ immutable and accessible by field names or indices, which might be useful::
 
 
     def namedtuplefetchall(cursor):
-        "Return all rows from a cursor as a namedtuple"
+        """
+        Return all rows from a cursor as a namedtuple.
+        Assume the column names are unique.
+        """
         desc = cursor.description
         nt_result = namedtuple("Result", [col[0] for col in desc])
         return [nt_result(*row) for row in cursor.fetchall()]
 
+The ``dictfetchall()`` and ``namedtuplefetchall()`` examples assume unique
+column names, since a cursor cannot distinguish columns from different tables.
+
 Here is an example of the difference between the three:
 
 .. code-block:: pycon