]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fixed example of upsert in the HasCTE.cte docstring by
authorVladimir Magamedov <vladimir@magamedov.com>
Sat, 27 Aug 2016 11:17:49 +0000 (14:17 +0300)
committerVladimir Magamedov <vladimir@magamedov.com>
Sat, 27 Aug 2016 11:21:56 +0000 (14:21 +0300)
replacing it with more concrete and working example.

lib/sqlalchemy/sql/selectable.py

index 9770b11bbfd77e16d3e3f86c5d147901f639f0b5..11ef99f09f426d5c140a048abe98b39a67f0a217 100644 (file)
@@ -1555,29 +1555,39 @@ class HasCTE(object):
 
         Example 3, an upsert using UPDATE and INSERT with CTEs::
 
-            orders = table(
-                'orders',
-                column('region'),
-                column('amount'),
-                column('product'),
-                column('quantity')
+            from datetime import date
+            from sqlalchemy import (MetaData, Table, Column, Integer,
+                                    Date, select, literal, and_, exists)
+
+            metadata = MetaData()
+
+            visitors = Table('visitors', metadata,
+                Column('product_id', Integer, primary_key=True),
+                Column('date', Date, primary_key=True),
+                Column('count', Integer),
             )
 
-            upsert = (
-                orders.update()
-                .where(orders.c.region == 'Region1')
-                .values(amount=1.0, product='Product1', quantity=1)
-                .returning(*(orders.c._all_columns)).cte('upsert'))
+            # add 5 visitors for the product_id == 1
+            product_id = 1
+            day = date.today()
+            count = 5
+
+            update_cte = (
+                visitors.update()
+                .where(and_(visitors.c.product_id == product_id,
+                            visitors.c.date == day))
+                .values(count=visitors.c.count + count)
+                .returning(literal(1))
+                .cte('update_cte')
+            )
 
-            insert = orders.insert().from_select(
-                orders.c.keys(),
-                select([
-                    literal('Region1'), literal(1.0),
-                    literal('Product1'), literal(1)
-                ).where(exists(upsert.select()))
+            upsert = visitors.insert().from_select(
+                [visitors.c.product_id, visitors.c.date, visitors.c.count],
+                select([literal(product_id), literal(day), literal(count)])
+                    .where(~exists(update_cte.select()))
             )
 
-            connection.execute(insert)
+            connection.execute(upsert)
 
         .. seealso::