diff --git a/piccolo/query/methods/insert.py b/piccolo/query/methods/insert.py index 899d61cc7..311f66506 100644 --- a/piccolo/query/methods/insert.py +++ b/piccolo/query/methods/insert.py @@ -88,6 +88,10 @@ def on_conflict( ########################################################################### + def _validate(self): + if not self.add_delegate._add: + raise ValueError("No rows were specified to insert.") + def _raw_response_callback(self, results: list): """ Assign the ids of the created rows to the model instances. diff --git a/tests/table/test_insert.py b/tests/table/test_insert.py index 4970a2c7a..6b6d43a24 100644 --- a/tests/table/test_insert.py +++ b/tests/table/test_insert.py @@ -39,6 +39,15 @@ def test_add(self): self.assertIn("Rustaceans", names) + def test_no_rows(self): + with self.assertRaises(ValueError) as manager: + Band.insert().run_sync() + + self.assertEqual( + manager.exception.__str__(), + "No rows were specified to insert.", + ) + def test_incompatible_type(self): """ You shouldn't be able to add instances of a different table.