table.checks, table.column_checks, table.table_checks, closes #834

Refs #762
This commit is contained in:
Simon Willison 2026-08-11 21:58:00 -07:00
commit 3db0c57a3b
6 changed files with 775 additions and 1 deletions

View file

@ -9,6 +9,7 @@
Unreleased
----------
- New ``table.checks``, ``table.column_checks`` and ``table.table_checks`` introspection properties expose column-level and table-level ``CHECK`` constraints. (:issue:`834`)
- ``table.transform()`` now works for tables that are referenced by views. Previously the ``ALTER TABLE ... RENAME TO`` step raised ``no such table`` if a view referenced the table being transformed. View definitions are left unchanged - see :ref:`python_api_transform_views`. This also fixes a bug where ``transform(keep_table=...)`` silently rewrote dependent views to point at the frozen backup table instead of the live one. (:issue:`831`)
.. _v3_39_1:

View file

@ -2480,6 +2480,43 @@ Almost all SQLite tables have a ``rowid`` column, but a table with no explicitly
False
.. _python_api_introspection_checks:
.checks
-------
The ``.checks`` property returns the column-level and table-level ``CHECK`` constraints defined on a table, as a list of ``Check`` objects. Each object has ``check`` (the expression inside ``CHECK (...)``), ``name``, ``column`` and ``options`` attributes. ``column`` is an empty string for a table-level check. ``options`` contains a list of values only when a column check consists entirely of ``column IN (literal, ...)``. The original constraint fragment is available as ``sql``; ``start`` and ``end`` are its offsets within ``table.schema``.
.. code-block:: python
>>> db["scores"].checks
[Check(check='score > 0', name='positive', column='score', options=None),
Check(check='score <= maximum', name='within_maximum', column='', options=None)]
.. _python_api_introspection_column_checks:
.column_checks
--------------
The ``.column_checks`` property returns the column-level checks grouped by column name:
.. code-block:: python
>>> db["scores"].column_checks
{'score': [Check(check='score > 0', name='positive', column='score', options=None)]}
.. _python_api_introspection_table_checks:
.table_checks
-------------
The ``.table_checks`` property returns only the table-level checks:
.. code-block:: python
>>> db["scores"].table_checks
[Check(check='score <= maximum', name='within_maximum', column='', options=None)]
.. _python_api_introspection_foreign_keys:
.foreign_keys