You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I attempted to create empty tables, similar to CREATE TABLE in SQL. TinyDB currently does not persist empty tables:
My initial approach didn't create a table because Table does not call _update_table() when creating an instance.
In [1]: fromtinydbimportTinyDBIn [2]: db=TinyDB("example.json")
In [3]: t=db.table("emptytbl")
In [4]: db.close()
In [5]: frompathlibimportPathIn [6]: Path("example.json").read_text()
Out[6]: ''
Workaround with adding truncate() call to ensure that the table gets written.
In [1]: fromtinydbimportTinyDBIn [2]: db=TinyDB("example.json")
In [3]: t=db.table("emptytbl")
In [4]: t.truncate()
In [5]: db.close()
In [6]: frompathlibimportPathIn [7]: Path("example.json").read_text()
Out[7]: '{"emptytbl": {}}'
I'm new to the project so I'm not sure if this is intentional. I propose adding a flag to Table, persist_empty: bool = False, and if the flag is true then call _update_table to ensure an empty table gets immediately written.
I'm happy to submit a PR if that makes sense.
The text was updated successfully, but these errors were encountered:
That's an interesting point. You're right, right now calling table(...) is a lazy operation that does not touch the file system until data is written to the table for the first time. I'd be happy to accept a PR for this 🙂
dmarsic
added a commit
to dmarsic/tinydb
that referenced
this issue
Feb 27, 2023
Hi,
I attempted to create empty tables, similar to
CREATE TABLE
in SQL. TinyDB currently does not persist empty tables:Table
does not call_update_table()
when creating an instance.truncate()
call to ensure that the table gets written.I'm new to the project so I'm not sure if this is intentional. I propose adding a flag to
Table
,persist_empty: bool = False
, and if the flag is true then call_update_table
to ensure an empty table gets immediately written.I'm happy to submit a PR if that makes sense.
The text was updated successfully, but these errors were encountered: