Add more STRICT table support (#604)

* Add more STRICT table support per https://github.com/simonw/sqlite-utils/issues/344#issuecomment-982014776.
* Make `table.transform()` preserve STRICT mode.
* Fix mypy failures in PR #604
* Link to SQLITE strict page in a few places
This commit is contained in:
Taj Khattra 2023-12-07 21:05:27 -08:00 committed by GitHub
commit 1500c19bd0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 182 additions and 5 deletions

View file

@ -289,6 +289,7 @@ See :ref:`cli_inserting_data`, :ref:`cli_insert_csv_tsv`, :ref:`cli_insert_unstr
--analyze Run ANALYZE at the end of this operation
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
--silent Do not show progress bar
--strict Apply STRICT mode to created table
--ignore Ignore records if pk already exists
--replace Replace records if pk already exists
--truncate Truncate table before inserting records, if table
@ -345,6 +346,7 @@ See :ref:`cli_upsert`.
--analyze Run ANALYZE at the end of this operation
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
--silent Do not show progress bar
--strict Apply STRICT mode to created table
-h, --help Show this message and exit.
@ -920,6 +922,7 @@ See :ref:`cli_create_table`.
--replace If table already exists, replace it
--transform If table already exists, try to transform the schema
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
--strict Apply STRICT mode to created table
-h, --help Show this message and exit.

View file

@ -1972,6 +1972,25 @@ You can specify foreign key relationships between the tables you are creating us
[author_id] INTEGER REFERENCES [authors]([id])
)
You can create a table in `SQLite STRICT mode <https://www.sqlite.org/stricttables.html>`__ using ``--strict``:
.. code-block:: bash
sqlite-utils create-table mydb.db mytable id integer name text --strict
.. code-block:: bash
sqlite-utils tables mydb.db --schema -t
.. code-block:: output
table schema
------- ------------------------
mytable CREATE TABLE [mytable] (
[id] INTEGER,
[name] TEXT
) STRICT
If a table with the same name already exists, you will get an error. You can choose to silently ignore this error with ``--ignore``, or you can replace the existing table with a new, empty table using ``--replace``.
You can also pass ``--transform`` to transform the existing table to match the new schema. See :ref:`python_api_explicit_create` in the Python library documentation for details of how this option works.
@ -2018,7 +2037,7 @@ Use ``--ignore`` to ignore the error if the table does not exist.
Transforming tables
===================
The ``transform`` command allows you to apply complex transformations to a table that cannot be implemented using a regular SQLite ``ALTER TABLE`` command. See :ref:`python_api_transform` for details of how this works.
The ``transform`` command allows you to apply complex transformations to a table that cannot be implemented using a regular SQLite ``ALTER TABLE`` command. See :ref:`python_api_transform` for details of how this works. The ``transform`` command preserves a table's ``STRICT`` mode.
.. code-block:: bash

View file

@ -117,6 +117,12 @@ By default, any :ref:`sqlite-utils plugins <plugins>` that implement the :ref:`p
db = Database(memory=True, execute_plugins=False)
You can pass ``strict=True`` to enable `SQLite STRICT mode <https://www.sqlite.org/stricttables.html>`__ for all tables created using this database object:
.. code-block:: python
db = Database("my_database.db", strict=True)
.. _python_api_attach:
Attaching additional databases
@ -581,6 +587,15 @@ The ``transform=True`` option will update the table schema if any of the followi
Changes to ``foreign_keys=`` are not currently detected and applied by ``transform=True``.
You can pass ``strict=True`` to create a table in ``STRICT`` mode:
.. code-block:: python
db["cats"].create({
"id": int,
"name": str,
}, strict=True)
.. _python_api_compound_primary_keys:
Compound primary keys
@ -661,7 +676,7 @@ You can set default values for these methods by accessing the table through the
# Now you can call .insert() like so:
table.insert({"id": 1, "name": "Tracy", "score": 5})
The configuration options that can be specified in this way are ``pk``, ``foreign_keys``, ``column_order``, ``not_null``, ``defaults``, ``batch_size``, ``hash_id``, ``hash_id_columns``, ``alter``, ``ignore``, ``replace``, ``extracts``, ``conversions``, ``columns``. These are all documented below.
The configuration options that can be specified in this way are ``pk``, ``foreign_keys``, ``column_order``, ``not_null``, ``defaults``, ``batch_size``, ``hash_id``, ``hash_id_columns``, ``alter``, ``ignore``, ``replace``, ``extracts``, ``conversions``, ``columns``, ``strict``. These are all documented below.
.. _python_api_defaults_not_null:
@ -1011,6 +1026,7 @@ The first time this is called the record will be created for ``name="Palm"``. An
- ``extracts``
- ``conversions``
- ``columns``
- ``strict``
.. _python_api_extracts: