From e3348df335d59a5660df04c13f6ccac2bbd2b130 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Chwa=C5=82a?= Date: Fri, 4 Apr 2025 22:00:50 +0200 Subject: [PATCH] Fix: PostgreSQLQueryBuilder returns invalid query with no columns to update #832 --- pypika/dialects.py | 3 +++ pypika/tests/test_inserts.py | 10 ++++++++++ pypika/tests/test_updates.py | 5 +++++ 3 files changed, 18 insertions(+) diff --git a/pypika/dialects.py b/pypika/dialects.py index 57955d85..a16e6544 100644 --- a/pypika/dialects.py +++ b/pypika/dialects.py @@ -661,6 +661,9 @@ def get_sql(self, with_alias: bool = False, subquery: bool = False, **kwargs: An querystring = super(PostgreSQLQueryBuilder, self).get_sql(with_alias, subquery, **kwargs) + if querystring == '': + return '' + querystring += self._on_conflict_sql(**kwargs) querystring += self._on_conflict_action_sql(**kwargs) diff --git a/pypika/tests/test_inserts.py b/pypika/tests/test_inserts.py index f86efd7a..df8eb712 100644 --- a/pypika/tests/test_inserts.py +++ b/pypika/tests/test_inserts.py @@ -467,6 +467,11 @@ def test_on_conflict_where_complex(self): str(qs), ) + def test_on_conflict_no_inserts(self): + query = PostgreSQLQuery.into(self.table_abc).on_conflict("id").do_nothing() + + self.assertEqual('', str(query)) + class PostgresInsertIntoReturningTests(unittest.TestCase): table_abc = Table("abc") @@ -567,6 +572,11 @@ def test_insert_returning_from_other_table(self): with self.assertRaises(QueryException): PostgreSQLQuery.into(self.table_abc).insert(1).returning(table_cba.id) + def test_insert_returning_no_inserts(self): + query = PostgreSQLQuery.into(self.table_abc).returning(self.table_abc.star) + + self.assertEqual('', str(query)) + class InsertIntoOnDuplicateTests(unittest.TestCase): table_abc = Table("abc") diff --git a/pypika/tests/test_updates.py b/pypika/tests/test_updates.py index fb9d714d..3fb6e970 100644 --- a/pypika/tests/test_updates.py +++ b/pypika/tests/test_updates.py @@ -152,6 +152,11 @@ def test_update_returning_star(self): self.assertEqual('UPDATE "abc" SET "foo"=\'bar\' WHERE "foo"=0 RETURNING *', str(q)) + def test_update_returning_no_updates(self): + q = PostgreSQLQuery.update(self.table_abc).returning(self.table_abc.star) + + self.assertEqual('', str(q)) + class SQLLiteUpdateTests(unittest.TestCase): table_abc = Table("abc")