Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: MemoryStorage incorrectly keeps references to nested dicts #551

Open
helgridly opened this issue Jan 3, 2024 · 1 comment
Open

Comments

@helgridly
Copy link

helgridly commented Jan 3, 2024

  • Make a Python object obj with a nested dict
  • Insert it into the database
  • Change a value in obj's nested dict
  • Make a query that retrieves obj
  • It has your change

See below:

from tinydb import TinyDB, Query
from tinydb.storages import MemoryStorage

db = TinyDB(storage=MemoryStorage)
t = db.table("test", cache_size=0)
obj = {"a": "a", "b" : "b", "nested": {"c": "c"}}
t.insert(obj)
print("inserted", obj)

obj['nested']['c'] = "X"
obj2 = t.get(Query().a == "a")
print("retrieved", obj2)
inserted {'a': 'a', 'b': 'b', 'nested': {'c': 'c'}}
retrieved {'a': 'a', 'b': 'b', 'nested': {'c': 'X'}}

I'm guessing somewhere the nested dict is being saved as a reference, not as a copy. I've confirmed it's not the query cache by setting it to zero.

This doesn't happen with normal JSON storage.

@helgridly helgridly changed the title MemoryStorage overwrites nested dicts on source objects MemoryStorage keeps references to nested dicts Jan 11, 2024
@helgridly helgridly changed the title MemoryStorage keeps references to nested dicts bug: MemoryStorage incorrectly keeps references to nested dicts Jan 11, 2024
@msiemens
Copy link
Owner

msiemens commented Oct 7, 2024

Hey @helgridly, you're absolutely right that TinyDB's MemoryStorage stores a reference, not a copy of the data that you insert. The correct solution to this would be to always create a deep copy of all data that is inserted to the database, but I'm somewhat conflicted as it also technically is a performance penalty for every write operation. Would it help to describe this behavior in the documentation, or do you think should be fixed on a deeper level?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants