db[table].create(..., transform=True) and create-table --transform

Closes #467
This commit is contained in:
Simon Willison 2022-08-27 16:17:55 -07:00 committed by GitHub
commit 104f37fa4d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 189 additions and 11 deletions

View file

@ -554,6 +554,25 @@ To do nothing if the table already exists, add ``if_not_exists=True``:
"name": str,
}, pk="id", if_not_exists=True)
You can also pass ``transform=True`` to have any existing tables :ref:`transformed <python_api_transform>` to match your new table specification. This is a **dangerous operation** as it may drop columns that are no longer listed in your call to ``.create()``, so be careful when running this.
.. code-block:: python
db["cats"].create({
"id": int,
"name": str,
"weight": float,
}, pk="id", transform=True)
The ``transform=True`` option will update the table schema if any of the following have changed:
- The specified columns or their types
- The specified primary key
- The order of the columns, defined using ``column_order=``
- The ``not_null=`` or ``defaults=`` arguments
Changes to ``foreign_keys=`` are not currently detected and applied by ``transform=True``.
.. _python_api_compound_primary_keys:
Compound primary keys