3 ms·
I wrote a version of that as part of my sqlite-utils Python library: https://sqlite-utils.datasette.io/en/stable/python-api.html#explicitly-creating-a-table htt
by simonw 2y ago
I wrote a version of that as part of my sqlite-utils Python library: https://sqlite-utils.datasette.io/en/stable/python-api.html#explicitly-creating-a-table https://sqlite-utils.datasette.io/en/stable/python-api.html#...
You can do something like this:
db["cats"].create({
"id": int,
"name": str,
"weight": float,
}, pk="id", transform=True)
The transform=True parameter means "if the table already exists but does not match the provided schema, transform it to add missing columns etc".
I don't generally recommend it though, it's a risky way of working compared to stateful migrations.
Here are the tests for that option: https://github.com/simonw/sqlite-utils/blob/577078fe01da6d876edda6f3ccbd1b5d074aeb6e/tests/test_create.py#L1234 https://github.com/simonw/sqlite-utils/blob/577078fe01da6d87... - and the accompanying issue: https://github.com/simonw/sqlite-utils/issues/467 https://github.com/simonw/sqlite-utils/issues/467