add possibility of compound foreign keys to docs

This commit is contained in:
David Kane 2020-11-16 00:40:04 +00:00
commit 8576ca873d

View file

@ -383,6 +383,17 @@ You can leave off the third item in the tuple to have the referenced column auto
], foreign_keys=[ ], foreign_keys=[
("author_id", "authors") ("author_id", "authors")
]) ])
Compound foreign keys can be created by passing in tuples of columns rather than strings:
.. code-block:: python
foreign_keys=[
(("author_id", "person_id"), "authors", ("id", "person_id"))
]
This means that the ``author_id`` and ``person_id`` columns should be a compound foreign key that references the ``id`` and ``person_id`` columns in the ``authors`` table.
.. _python_api_table_configuration: .. _python_api_table_configuration:
@ -867,6 +878,20 @@ The ``table.add_foreign_key(column, other_table, other_column)`` method takes th
This method first checks that the specified foreign key references tables and columns that exist and does not clash with an existing foreign key. It will raise a ``sqlite_utils.db.AlterError`` exception if these checks fail. This method first checks that the specified foreign key references tables and columns that exist and does not clash with an existing foreign key. It will raise a ``sqlite_utils.db.AlterError`` exception if these checks fail.
You can add compound foreign keys by passing a tuple of column names. For example:
.. code-block:: python
db["authors"].insert_all([
{"id": 1, "person_id": 1, "name": "Sally"},
{"id": 2, "person_id": 2, "name": "Asheesh"}
], pk="id")
db["books"].insert_all([
{"title": "Hedgehogs of the world", "author_id": 1, "person_id": 1},
{"title": "How to train your wolf", "author_id": 2, "person_id": 2},
])
db["books"].add_foreign_key(("author_id", "person_id"), "authors", ("id", "person_id"))
To ignore the case where the key already exists, use ``ignore=True``: To ignore the case where the key already exists, use ``ignore=True``:
.. code-block:: python .. code-block:: python