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 · 0 comments
Open

bug: MemoryStorage incorrectly keeps references to nested dicts #551

helgridly opened this issue Jan 3, 2024 · 0 comments

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
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

1 participant