From 77ed03652580c5db925729c573b76ca32393dc67 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 19 Feb 2014 15:18:22 -0500 Subject: [PATCH] - Fixed bug where calling :meth:`.Insert.values` with an empty list or tuple would raise an IndexError. It now produces an empty insert construct as would be the case with an empty dictionary. --- doc/build/changelog/changelog_08.rst | 9 +++++++++ lib/sqlalchemy/sql/dml.py | 1 + test/sql/test_insert.py | 23 +++++++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/doc/build/changelog/changelog_08.rst b/doc/build/changelog/changelog_08.rst index 4e91839cc5..aabcdf3c85 100644 --- a/doc/build/changelog/changelog_08.rst +++ b/doc/build/changelog/changelog_08.rst @@ -11,6 +11,15 @@ .. changelog:: :version: 0.8.5 + .. change:: + :tags: bug, sql + :versions: 0.9.3 + :tickets: 2944 + + Fixed bug where calling :meth:`.Insert.values` with an empty list + or tuple would raise an IndexError. It now produces an empty + insert construct as would be the case with an empty dictionary. + .. change:: :tags: bug, engine, pool :versions: 0.9.3 diff --git a/lib/sqlalchemy/sql/dml.py b/lib/sqlalchemy/sql/dml.py index 854b894ee6..098f2d5842 100644 --- a/lib/sqlalchemy/sql/dml.py +++ b/lib/sqlalchemy/sql/dml.py @@ -37,6 +37,7 @@ class UpdateBase(DialectKWArgs, HasPrefixes, Executable, ClauseElement): return p if isinstance(parameters, (list, tuple)) and \ + parameters and \ isinstance(parameters[0], (list, tuple, dict)): if not self._supports_multi_parameters: diff --git a/test/sql/test_insert.py b/test/sql/test_insert.py index 5c3b9b6c98..8a5c7cd83b 100644 --- a/test/sql/test_insert.py +++ b/test/sql/test_insert.py @@ -244,6 +244,29 @@ class EmptyTest(_InsertTestBase, fixtures.TablesTest, AssertsCompiledSQL): "settings does not support empty inserts.", stmt.compile, dialect=dialect) + def _test_insert_with_empty_collection_values(self, collection): + table1 = self.tables.mytable + + ins = table1.insert().values(collection) + + self.assert_compile(ins, + 'INSERT INTO mytable () VALUES ()', + checkparams={}) + + # empty dict populates on next values call + self.assert_compile(ins.values(myid=3), + 'INSERT INTO mytable (myid) VALUES (:myid)', + checkparams={'myid': 3}) + + def test_insert_with_empty_list_values(self): + self._test_insert_with_empty_collection_values([]) + + def test_insert_with_empty_dict_values(self): + self._test_insert_with_empty_collection_values({}) + + def test_insert_with_empty_tuple_values(self): + self._test_insert_with_empty_collection_values(()) + class MultirowTest(_InsertTestBase, fixtures.TablesTest, AssertsCompiledSQL): __dialect__ = 'default' -- 2.47.3