Skip to content

Cannot keep reference to table without keeping a reference to the database #92

@michou

Description

@michou

This throws a ValueError: I/O operation on closed file.:

db = TinyDB('./db.json').table('foo')
db.insert({'something': 'else'})
db.insert({'int': 13})
print(db.search(Query()['int'] == 13))
print(db.all())

while this works:

db = TinyDB('./db.json')
table = db.table('foo')
table.insert({'something': 'else'})
table.insert({'int': 13})
print(table.search(Query()['int'] == 13))
print(table.all())

It took me and @alexghr a couple of hours to figure out it had to do with the garbage collector deleting the db object (and thus closing the database).
Would it make sense to have the documentation explicitly state that losing the reference to the database will mess up your tables?

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