Flexible tables allow records to have arbitrary sets of fields without changing the table structure.
Enable flexible fields by passing flexible=True:
db.create_table(
"players",
("un", "login", str),
flexible=True,
)
Declared fields are stored in regular SQLite columns, while additional fields are stored as JSON in the internal _data column.
Inserting and reading flexible fields works just like using regular columns.
db.insert(
"players",
login="ada",
level=5,
settings={"theme": "dark", "sound": True},
)
player = db.select_one("players", "login = ?", "ada")
print(player)
# {
# 'login': 'ada',
# 'level': 5,
# 'settings': {'theme': 'dark', 'sound': True}
# }
LiteCrate automatically combines values from regular columns and JSON. The internal _data column is not included in the result.
The update() method can change existing fields and add new ones without affecting the other values in a record.
However, dictionaries and lists are replaced entirely, rather than merged with their previous contents.
db.update(
"players",
"login = ?",
"ada",
level=10,
nickname="Pilot",
settings={"theme": "light"},
)
player = db.select_one("players", "login = ?", "ada")
print(player)
# {
# 'login': 'ada',
# 'level': 10,
# 'settings': {'theme': 'light'},
# 'nickname': 'Pilot'
# }
In this example, nickname was added, level was updated, and the settings dictionary was replaced entirely. As a result, the previous sound key is gone.
In flexible mode, additional fields are created automatically. This also applies to misspelled names.
db.update("players", "login = ?", "ada", levle=20)
player = db.select_one("players", "login = ?", "ada")
print(player["level"])
# 10
print(player["levle"])
# 20
The typo levle did not raise an exception: instead of changing level, the library created a new field.
LiteCrate lets you use ordinary SQL conditions to search additional fields.
db.select("players", "level >= ?", 5)
db.count("players", "nickname IS NOT NULL")
The library automatically determines which fields are regular columns and which are stored in JSON.
To search inside nested JSON objects, you can use SQLite’s json_extract() function:
db.select(
"players",
"json_extract(_data, '$.settings.theme') = ?",
"light",
)
However, direct access to _data is not recommended because it ties your code to LiteCrate’s internal storage structure.
Additional fields support JSON-compatible values: strings, finite numbers, bool, None, lists, and dictionaries with string keys.
Keep these details in mind when working with flexible fields:
None and missing fields. None is stored as JSON null. Missing fields are not included in the returned dictionary. A field IS NULL condition may match both cases.UNIQUE and NOT NULL constraints.bytes type is not supported in JSON. Use a separate column for binary data.For more on declaring columns, see Tables and data types.