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
{{ message }}
Repository navigation
Commit fcfccea
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: docs/changelog.rst
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,7 @@ Unreleased
10
10
----------
11
11
12
12
- New ``table.checks``, ``table.column_checks`` and ``table.table_checks`` introspection properties expose column-level and table-level ``CHECK`` constraints. (:issue:`834`)
13
+
- New ``sqlite_utils.ANY`` marker type for creating and introspecting SQLite ``ANY`` columns. The Python API and CLI can create, add and transform these columns, and ``table.transform()`` and ``table.extract()`` now preserve ``ANY`` columns and their values in ``STRICT`` tables. (:issue:`790`)
13
14
- ``table.transform()`` now preserves ``CHECK`` constraints, including comments within their expressions. Renaming a column rewrites identifier references in checks without changing string literals or function names. Dropping a column drops a check owned by that column, and raises ``TransformError`` if a remaining check depends on it. (:issue:`762`)
14
15
- ``table.transform()`` now preserves comments immediately before or after column definitions. These comments move with the column if it is renamed or reordered, and are removed if the column is dropped. (:issue:`762`)
15
16
- ``table.transform(rename=...)`` now preserves explicit indexes on renamed columns by dropping and recreating those indexes against the new column names. Previously this raised a ``TransformError``. (:issue:`822`)
Copy file name to clipboardExpand all lines: docs/cli.rst
+15-2Lines changed: 15 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1390,7 +1390,14 @@ Use ``--type column-name type`` to override the type automatically chosen when t
1390
1390
1391
1391
This is useful for values such as ZIP codes, which may look like integers but should be stored as ``TEXT`` to preserve leading zeros.
1392
1392
1393
-
The column type should be one of ``TEXT``, ``INTEGER``, ``FLOAT``, ``REAL`` or ``BLOB``. Column types are matched case-insensitively.
1393
+
The column type should be one of ``TEXT``, ``INTEGER``, ``FLOAT``, ``REAL``, ``BLOB`` or ``ANY``. Column types are matched case-insensitively.
1394
+
1395
+
``ANY`` is especially useful with ``--strict``. An ``ANY`` column in a strict table preserves values without coercion, so text such as ``000123`` remains text instead of being converted to an integer:
As with detected column types, ``--type`` only affects tables created by the command. If the table already exists, its existing column types are left unchanged.
1396
1403
@@ -2141,6 +2148,12 @@ You can create a table in `SQLite STRICT mode <https://www.sqlite.org/stricttabl
2141
2148
2142
2149
sqlite-utils create-table mydb.db mytable id integer name text --strict
2143
2150
2151
+
Use the ``any`` type for a strict column that should accept integers, floating point values, text, binary data or null without coercion:
2152
+
2153
+
.. code-block:: bash
2154
+
2155
+
sqlite-utils create-table events.db events id integer payload any --strict
2156
+
2144
2157
.. code-block:: bash
2145
2158
2146
2159
sqlite-utils tables mydb.db --schema -t
@@ -2223,7 +2236,7 @@ The ``transform`` command allows you to apply complex transformations to a table
2223
2236
Every option for this table (with the exception of ``--pk-none``) can be specified multiple times. The options are as follows:
2224
2237
2225
2238
``--type column-name new-type``
2226
-
Change the type of the specified column. Valid types are ``integer``, ``text``, ``float``, ``blob``.
2239
+
Change the type of the specified column. Valid types are ``integer``, ``text``, ``float``, ``real``, ``blob`` and ``any``.
Copy file name to clipboardExpand all lines: docs/python-api.rst
+22-1Lines changed: 22 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -828,6 +828,19 @@ You can pass ``strict=True`` to create a table in ``STRICT`` mode:
828
828
"name": str,
829
829
}, strict=True)
830
830
831
+
SQLite ``STRICT`` tables can use the ``ANY`` column type for values that should retain their exact SQLite storage class without coercion. Use the ``sqlite_utils.ANY`` marker type:
832
+
833
+
.. code-block:: python
834
+
835
+
import sqlite_utils
836
+
837
+
db.table("events").create({
838
+
"id": int,
839
+
"payload": sqlite_utils.ANY,
840
+
}, pk="id", strict=True)
841
+
842
+
An ``ANY`` column can store integers, floating point values, text, binary data or ``None``. In a ``STRICT`` table a text value such as ``"000123"`` remains text with its leading zeroes intact. SQLite also accepts ``ANY`` columns in ordinary non-``STRICT`` tables, but those columns apply numeric affinity and would store that same value as the integer ``123``.
843
+
831
844
.. note::
832
845
In the CLI: :ref:`sqlite-utils create-table <cli_create_table>`
833
846
@@ -1569,7 +1582,7 @@ You can specify the ``col_type`` argument either using a SQLite type as a string
1569
1582
1570
1583
The ``col_type`` is optional - if you omit it the type of ``TEXT`` will be used.
1571
1584
1572
-
SQLite types you can specify are ``"TEXT"``, ``"INTEGER"``, ``"FLOAT"``, ``"REAL"``or ``"BLOB"``.
1585
+
SQLite types you can specify are ``"TEXT"``, ``"INTEGER"``, ``"FLOAT"``, ``"REAL"``, ``"BLOB"`` or ``"ANY"``. You can use the ``sqlite_utils.ANY`` marker instead of the ``"ANY"`` string.
1573
1586
1574
1587
If you pass a Python type, it will be mapped to SQLite types as shown here::
1575
1588
@@ -1582,6 +1595,7 @@ If you pass a Python type, it will be mapped to SQLite types as shown here::
1582
1595
datetime.date: "TEXT"
1583
1596
datetime.time: "TEXT"
1584
1597
datetime.timedelta: "TEXT"
1598
+
sqlite_utils.ANY: "ANY"
1585
1599
1586
1600
# If numpy is installed
1587
1601
np.int8: "INTEGER"
@@ -1831,6 +1845,8 @@ Pass ``strict=False`` to convert a strict table back to a regular non-strict tab
1831
1845
1832
1846
table.transform(strict=False)
1833
1847
1848
+
If the table has ``ANY`` columns, converting it to non-strict mode can coerce text values that look numeric. For example, SQLite converts ``"000123"`` to the integer ``123`` when copying it into an ordinary ``ANY`` column. This is SQLite's documented distinction between `STRICT and ordinary ANY columns <https://www.sqlite.org/stricttables.html#the_any_datatype>`__.
1849
+
1834
1850
The default is ``strict=None``, which preserves the table's existing strict mode.
1835
1851
1836
1852
Passing ``strict=True`` raises ``sqlite_utils.db.TransformError`` if the available SQLite version does not support strict tables.
@@ -2458,6 +2474,11 @@ The ``.columns_dict`` property returns a dictionary version of the columns with
2458
2474
>>> db.table("PlantType").columns_dict
2459
2475
{'id': <class'int'>, 'value': <class'str'>}
2460
2476
2477
+
SQLite ``ANY`` columns are represented by the ``sqlite_utils.ANY`` marker type::
0 commit comments