Skip to content

Commit 0a567b7

Browse files
committed
Add documentation for update_incoming_fks parameter
Document the new update_incoming_fks parameter in: - Python API docs (python-api.rst): New section explaining usage - CLI docs (cli.rst): Document --update-incoming-fks flag
1 parent 798149b commit 0a567b7

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

docs/cli.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,6 +2113,9 @@ Every option for this table (with the exception of ``--pk-none``) can be specifi
21132113
``--add-foreign-key column other_table other_column``
21142114
Add a foreign key constraint to ``column`` pointing to ``other_table.other_column``.
21152115

2116+
``--update-incoming-fks``
2117+
When renaming columns, automatically update foreign key constraints in other tables that reference the renamed columns. For example, if ``books.author_id`` references ``authors.id`` and you rename ``authors.id`` to ``authors.author_pk``, this flag will also update the foreign key in ``books`` to reference the new column name.
2118+
21162119
If you want to see the SQL that will be executed to make the change without actually executing it, add the ``--sql`` flag. For example:
21172120

21182121
.. code-block:: bash

docs/python-api.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,6 +1631,31 @@ This example drops two foreign keys - the one from ``places.country`` to ``count
16311631
drop_foreign_keys=("country", "continent")
16321632
)
16331633
1634+
.. _python_api_transform_update_incoming_fks:
1635+
1636+
Updating foreign keys in other tables
1637+
-------------------------------------
1638+
1639+
When renaming columns that are referenced by foreign keys in other tables, you can use the ``update_incoming_fks=True`` parameter to automatically update those foreign key constraints.
1640+
1641+
For example, if you have a ``books`` table with a foreign key from ``books.author_id`` to ``authors.id``, and you want to rename ``authors.id`` to ``authors.author_pk``:
1642+
1643+
.. code-block:: python
1644+
1645+
db["authors"].transform(
1646+
rename={"id": "author_pk"},
1647+
update_incoming_fks=True,
1648+
)
1649+
1650+
This will rename the column in the ``authors`` table and also update the foreign key constraint in the ``books`` table to reference ``authors.author_pk`` instead of ``authors.id``.
1651+
1652+
Without ``update_incoming_fks=True``, this operation would fail with a foreign key mismatch error (if foreign key enforcement is enabled) because the ``books`` table would still reference the old column name.
1653+
1654+
This parameter also correctly handles:
1655+
1656+
- Multiple tables referencing the renamed column
1657+
- Self-referential foreign keys (e.g., an ``employees.manager_id`` column referencing ``employees.id``)
1658+
16341659
.. _python_api_transform_sql:
16351660
16361661
Custom transformations with .transform_sql()

0 commit comments

Comments
 (0)