msiemens/tinydb

Wrong _last_id when opening an existing database

domeav opened this issue · 3 comments

TinyDB version 2.0.1. My interpreter session:

from tinydb import TinyDB
db = TinyDB('my_existing_db.json')
len(db) # => 314
print db._last_id # => 99 (which is obviously wrong, as _get_next_id will return an existing id, messing with existing data)

Ids are actually sorted alphabetically.Table.init, so '99' is the last one. A workaround (albeit perhaps not the best one) is to replace line 191:

all_ids = sorted(self._read().keys())

by the following:

all_ids = sorted(self._read().keys(), key=int)

I find it strange I'm the first to notice this issue.

Thanks for reporting and finding the cause!

Fixed in the just released v2.1.0.

Wow, that was fast :-)