Create table if_not_exists=True argument, closes #397

This commit is contained in:
Simon Willison 2022-02-05 17:28:53 -08:00
commit aa24903113
3 changed files with 33 additions and 3 deletions

View file

@ -522,7 +522,9 @@ This will create a table with the following schema:
Explicitly creating a table
---------------------------
You can directly create a new table without inserting any data into it using the ``.create()`` method::
You can directly create a new table without inserting any data into it using the ``.create()`` method:
.. code-block:: python
db["cats"].create({
"id": int,
@ -534,6 +536,17 @@ The first argument here is a dictionary specifying the columns you would like to
This method takes optional arguments ``pk=``, ``column_order=``, ``foreign_keys=``, ``not_null=set()`` and ``defaults=dict()`` - explained below.
A ``sqlite_utils.utils.sqlite3.OperationalError`` will be raised if a table of that name already exists.
To do nothing if the table already exists, add ``if_not_exists=True``:
.. code-block:: python
db["cats"].create({
"id": int,
"name": str,
}, pk="id", if_not_exists=True)
.. _python_api_compound_primary_keys:
Compound primary keys