@@ -1117,7 +1117,7 @@ def simple_insert_many_txn(
11171117 txn : LoggingTransaction ,
11181118 table : str ,
11191119 keys : Collection [str ],
1120- values : Iterable [Iterable [Any ]],
1120+ values : Collection [Iterable [Any ]],
11211121 ) -> None :
11221122 """Executes an INSERT query on the named table.
11231123
@@ -1130,6 +1130,9 @@ def simple_insert_many_txn(
11301130 keys: list of column names
11311131 values: for each row, a list of values in the same order as `keys`
11321132 """
1133+ # If there's nothing to insert, then skip executing the query.
1134+ if not values :
1135+ return
11331136
11341137 if isinstance (txn .database_engine , PostgresEngine ):
11351138 # We use `execute_values` as it can be a lot faster than `execute_batch`,
@@ -1455,7 +1458,7 @@ def simple_upsert_many_txn(
14551458 key_names : Collection [str ],
14561459 key_values : Collection [Iterable [Any ]],
14571460 value_names : Collection [str ],
1458- value_values : Iterable [Iterable [Any ]],
1461+ value_values : Collection [Iterable [Any ]],
14591462 ) -> None :
14601463 """
14611464 Upsert, many times.
@@ -1468,6 +1471,19 @@ def simple_upsert_many_txn(
14681471 value_values: A list of each row's value column values.
14691472 Ignored if value_names is empty.
14701473 """
1474+ # If there's nothing to upsert, then skip executing the query.
1475+ if not key_values :
1476+ return
1477+
1478+ # No value columns, therefore make a blank list so that the following
1479+ # zip() works correctly.
1480+ if not value_names :
1481+ value_values = [() for x in range (len (key_values ))]
1482+ elif len (value_values ) != len (key_values ):
1483+ raise ValueError (
1484+ f"{ len (key_values )} key rows and { len (value_values )} value rows: should be the same number."
1485+ )
1486+
14711487 if table not in self ._unsafe_to_upsert_tables :
14721488 return self .simple_upsert_many_txn_native_upsert (
14731489 txn , table , key_names , key_values , value_names , value_values
@@ -1502,10 +1518,6 @@ def simple_upsert_many_txn_emulated(
15021518 value_values: A list of each row's value column values.
15031519 Ignored if value_names is empty.
15041520 """
1505- # No value columns, therefore make a blank list so that the following
1506- # zip() works correctly.
1507- if not value_names :
1508- value_values = [() for x in range (len (key_values ))]
15091521
15101522 # Lock the table just once, to prevent it being done once per row.
15111523 # Note that, according to Postgres' documentation, once obtained,
@@ -1543,10 +1555,7 @@ def simple_upsert_many_txn_native_upsert(
15431555 allnames .extend (value_names )
15441556
15451557 if not value_names :
1546- # No value columns, therefore make a blank list so that the
1547- # following zip() works correctly.
15481558 latter = "NOTHING"
1549- value_values = [() for x in range (len (key_values ))]
15501559 else :
15511560 latter = "UPDATE SET " + ", " .join (
15521561 k + "=EXCLUDED." + k for k in value_names
@@ -1910,6 +1919,7 @@ def simple_select_many_txn(
19101919 Returns:
19111920 The results as a list of tuples.
19121921 """
1922+ # If there's nothing to select, then skip executing the query.
19131923 if not iterable :
19141924 return []
19151925
@@ -2044,6 +2054,9 @@ def simple_update_many_txn(
20442054 raise ValueError (
20452055 f"{ len (key_values )} key rows and { len (value_values )} value rows: should be the same number."
20462056 )
2057+ # If there is nothing to update, then skip executing the query.
2058+ if not key_values :
2059+ return
20472060
20482061 # List of tuples of (value values, then key values)
20492062 # (This matches the order needed for the query)
@@ -2278,6 +2291,7 @@ def simple_delete_many_txn(
22782291 Returns:
22792292 Number rows deleted
22802293 """
2294+ # If there's nothing to delete, then skip executing the query.
22812295 if not values :
22822296 return 0
22832297
0 commit comments