Join GitHub today
Default table query cache accidently (?) updated after repeated searches #132
In a program I have a database containing two tables: the default one and an "extra" one. When querying the tables one by one in a repeated manner, I noticed that the default table's query cache is accidently updated. Consider the following script:
I have defined two search functions for querying the database (t.i. finding entries in both tables).
Apparently, the default table's query cache is updated with elements of the extra table after the first search, hence yielding a duplicate result in the second search. Clearing the cache remedies the issue.
I was wondering whether this behaviour is intended. Obviously the issue can be avoided by selecting a function along
I'm using tinydb v3.2.1 (Python3).
Sidenote: I encountered this because my program design contains a search function that is supposed to always search the default table but search the extra table only if it was defined.
added a commit
Apr 10, 2017
Hey, thanks for reporting this. I've looked into this and here's what I found: When a query is cached, TinyDB returns the list of the cached results:
if cond in self._query_cache: return self._query_cache[cond]
Now, as default with Python, we return a reference to the cache list. When your code calls
I'd say that TinyDB should handle this case better as modifying the results list of a search shouldn't mess up the query cache. The fix is fairly simple (see a1fa290), we now just copy the result list before returning it. This should fix the problem.