Merge pull request #284 from al42and/upsert_id

Fix bug when UPSERTing a column named 'id'
This commit is contained in:
Friedrich Lindenberg 2019-07-13 14:28:32 +02:00 committed by GitHub
commit 6baff6fa34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 4 deletions

View File

@ -29,8 +29,14 @@ class Table(object):
self.name = normalize_table_name(table_name) self.name = normalize_table_name(table_name)
self._table = None self._table = None
self._indexes = [] self._indexes = []
if primary_id is not None:
self._primary_id = primary_id self._primary_id = primary_id
else:
self._primary_id = self.PRIMARY_DEFAULT
if primary_type is not None:
self._primary_type = primary_type self._primary_type = primary_type
else:
self._primary_type = Types.integer
self._auto_create = auto_create self._auto_create = auto_create
@property @property
@ -235,8 +241,8 @@ class Table(object):
if self._primary_id is not False: if self._primary_id is not False:
# This can go wrong on DBMS like MySQL and SQLite where # This can go wrong on DBMS like MySQL and SQLite where
# tables cannot have no columns. # tables cannot have no columns.
primary_id = self._primary_id or self.PRIMARY_DEFAULT primary_id = self._primary_id
primary_type = self._primary_type or Types.integer primary_type = self._primary_type
increment = primary_type in [Types.integer, Types.bigint] increment = primary_type in [Types.integer, Types.bigint]
column = Column(primary_id, primary_type, column = Column(primary_id, primary_type,
primary_key=True, primary_key=True,

View File

@ -232,6 +232,12 @@ class TableTestCase(unittest.TestCase):
) )
assert len(self.tbl) == len(TEST_DATA) + 1, len(self.tbl) assert len(self.tbl) == len(TEST_DATA) + 1, len(self.tbl)
def test_upsert_id(self):
table = self.db['banana_with_id']
data = dict(id=10, title='I am a banana!')
table.upsert(data, ['id'])
assert len(table) == 1, len(table)
def test_update_while_iter(self): def test_update_while_iter(self):
for row in self.tbl: for row in self.tbl:
row['foo'] = 'bar' row['foo'] = 'bar'