Add --update-incoming-fks CLI flag for transform command

When renaming columns that are referenced by foreign keys in other
tables, the --update-incoming-fks flag will automatically update
those FK constraints.

Example:
    sqlite-utils transform mydb.db authors \
        --rename id author_pk \
        --update-incoming-fks

This will rename the 'id' column to 'author_pk' and also update any
foreign key constraints in other tables (e.g., books.author_id)
that reference the renamed column.
This commit is contained in:
Claude 2026-01-21 14:58:11 +00:00
commit 798149b816
No known key found for this signature in database
2 changed files with 46 additions and 0 deletions

View file

@ -2545,6 +2545,11 @@ def schema(
multiple=True,
help="Drop foreign key constraint for this column",
)
@click.option(
"--update-incoming-fks",
is_flag=True,
help="Update foreign keys in other tables that reference renamed columns",
)
@click.option("--sql", is_flag=True, help="Output SQL without executing it")
@load_extension_option
def transform(
@ -2562,6 +2567,7 @@ def transform(
default_none,
add_foreign_keys,
drop_foreign_keys,
update_incoming_fks,
sql,
load_extension,
):
@ -2615,6 +2621,8 @@ def transform(
kwargs["drop_foreign_keys"] = drop_foreign_keys
if add_foreign_keys:
kwargs["add_foreign_keys"] = add_foreign_keys
if update_incoming_fks:
kwargs["update_incoming_fks"] = True
if sql:
for line in db.table(table).transform_sql(**kwargs):