From e51bd09a35d5c14f6aa5bdcc76262c14b5838d84 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 14 Jul 2019 21:03:57 -0700 Subject: [PATCH] Python library docs for compound primary keys --- docs/python-api.rst | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/docs/python-api.rst b/docs/python-api.rst index 0cc5007..1575349 100644 --- a/docs/python-api.rst +++ b/docs/python-api.rst @@ -147,6 +147,24 @@ 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. +.. _python_api_compound_primary_keys: + +Compound primary keys +--------------------- + +If you want to create a table with a compound primary key that spans multiple columns, you can do so by passing a tuple of column names to any of the methods that accept a ``pk=`` parameter. For example: + +.. code-block:: python + + db["cats"].create({ + "id": int, + "breed": str, + "name": str, + "weight": float, + }, pk=("breed", "id")) + +This also works for the ``.insert()``, ``.insert_all()``, ``.upsert()`` and ``.upsert_all()`` methods. + .. _python_api_foreign_keys: Specifying foreign keys