insert returns last_id (closes #4)

This commit is contained in:
Gregor Aisch 2013-04-06 03:06:51 +02:00
parent 393a0b887b
commit 875e0508f2
2 changed files with 4 additions and 2 deletions

View File

@ -66,7 +66,8 @@ class Table(object):
self._check_dropped()
if ensure:
self._ensure_columns(row, types=types)
self.database.engine.execute(self.table.insert(row))
res = self.database.engine.execute(self.table.insert(row))
return res.lastrowid
def insert_many(self, rows, chunk_size=1000, ensure=True, types={}):
"""

View File

@ -42,12 +42,13 @@ class TableTestCase(unittest.TestCase):
def test_insert(self):
assert len(self.tbl) == len(TEST_DATA), len(self.tbl)
self.tbl.insert({
last_id = self.tbl.insert({
'date': datetime(2011, 01, 02),
'temperature': -10,
'place': 'Berlin'}
)
assert len(self.tbl) == len(TEST_DATA)+1, len(self.tbl)
assert self.tbl.find_one(id=last_id)['place'] == 'Berlin'
def test_upsert(self):
self.tbl.upsert({