From 508e0256893d838e6eac7144b36277980de67056 Mon Sep 17 00:00:00 2001 From: Abhinav Upadhyay Date: Sat, 4 Jan 2014 11:53:15 +0530 Subject: [PATCH] Fix insert for tables that do not have any primary key. The insert method returns the value of the primary key for the inserted row. But for tables that do not have any primary key, it raises an IndexError, which is clearly wrong. At best it should return None in such cases and avoid raising any exception. --- dataset/persistence/table.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dataset/persistence/table.py b/dataset/persistence/table.py index c76b1fd..6737d5b 100644 --- a/dataset/persistence/table.py +++ b/dataset/persistence/table.py @@ -68,7 +68,8 @@ class Table(object): if ensure: self._ensure_columns(row, types=types) res = self.database.executable.execute(self.table.insert(row)) - return res.inserted_primary_key[0] + if len(res.inserted_primary_key) > 0: + return res.inserted_primary_key[0] def insert_many(self, rows, chunk_size=1000, ensure=True, types={}): """