File tree Expand file tree Collapse file tree 3 files changed +29
-13
lines changed Expand file tree Collapse file tree 3 files changed +29
-13
lines changed Original file line number Diff line number Diff line change @@ -1732,20 +1732,22 @@ def build_insert_queries_and_params(
17321732 queries_and_params .append ((sql , [record [col ] for col in pks ]))
17331733 # UPDATE [book] SET [name] = 'Programming' WHERE [id] = 1001;
17341734 set_cols = [col for col in all_columns if col not in pks ]
1735- sql2 = "UPDATE [{table}] SET {pairs} WHERE {wheres}" .format (
1736- table = self .name ,
1737- pairs = ", " .join (
1738- "[{}] = {}" .format (col , conversions .get (col , "?" ))
1739- for col in set_cols
1740- ),
1741- wheres = " AND " .join ("[{}] = ?" .format (pk ) for pk in pks ),
1742- )
1743- queries_and_params .append (
1744- (
1745- sql2 ,
1746- [record [col ] for col in set_cols ] + [record [pk ] for pk in pks ],
1735+ if set_cols :
1736+ sql2 = "UPDATE [{table}] SET {pairs} WHERE {wheres}" .format (
1737+ table = self .name ,
1738+ pairs = ", " .join (
1739+ "[{}] = {}" .format (col , conversions .get (col , "?" ))
1740+ for col in set_cols
1741+ ),
1742+ wheres = " AND " .join ("[{}] = ?" .format (pk ) for pk in pks ),
1743+ )
1744+ queries_and_params .append (
1745+ (
1746+ sql2 ,
1747+ [record [col ] for col in set_cols ]
1748+ + [record [pk ] for pk in pks ],
1749+ )
17471750 )
1748- )
17491751 # We can populate .last_pk right here
17501752 if num_records_processed == 1 :
17511753 self .last_pk = tuple (record [pk ] for pk in pks )
Original file line number Diff line number Diff line change @@ -991,6 +991,13 @@ def test_insert_all_empty_list(fresh_db):
991991 assert 1 == fresh_db ["t" ].count
992992
993993
994+ def test_insert_all_single_column (fresh_db ):
995+ table = fresh_db ["table" ]
996+ table .insert_all ([{"name" : "Cleo" }], pk = "name" )
997+ assert [{"name" : "Cleo" }] == list (table .rows )
998+ assert table .pks == ["name" ]
999+
1000+
9941001def test_create_with_a_null_column (fresh_db ):
9951002 record = {"name" : "Name" , "description" : None }
9961003 fresh_db ["t" ].insert (record )
Original file line number Diff line number Diff line change @@ -21,6 +21,13 @@ def test_upsert_all(fresh_db):
2121 assert table .last_pk is None
2222
2323
24+ def test_upsert_all_single_column (fresh_db ):
25+ table = fresh_db ["table" ]
26+ table .upsert_all ([{"name" : "Cleo" }], pk = "name" )
27+ assert [{"name" : "Cleo" }] == list (table .rows )
28+ assert table .pks == ["name" ]
29+
30+
2431def test_upsert_error_if_no_pk (fresh_db ):
2532 table = fresh_db ["table" ]
2633 with pytest .raises (PrimaryKeyRequired ):
You can’t perform that action at this time.
0 commit comments