Skip to content

bug: MemoryStorage incorrectly keeps references to nested dicts #551

@helgridly

Description

@helgridly
  • 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions