From ef249118243636186a644b73cbd65dad58a50221 Mon Sep 17 00:00:00 2001 From: PThorpe92 Date: Sun, 2 Nov 2025 17:10:01 -0500 Subject: [PATCH] Handle renaming child foreign keys on op_rename_table --- core/vdbe/execute.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/core/vdbe/execute.rs b/core/vdbe/execute.rs index 1ddb21553..a771799f5 100644 --- a/core/vdbe/execute.rs +++ b/core/vdbe/execute.rs @@ -8443,12 +8443,35 @@ pub fn op_rename_table( let Table::BTree(btree) = table else { panic!("only btree tables can be renamed"); }; - let btree = Arc::make_mut(btree); + // update this table's own foreign keys + for fk_arc in &mut btree.foreign_keys { + let fk = Arc::make_mut(fk_arc); + if normalize_ident(&fk.parent_table) == normalized_from { + fk.parent_table = normalized_to.clone(); + } + } + btree.name = normalized_to.to_owned(); } schema.tables.insert(normalized_to.to_owned(), table); + + for (tname, t_arc) in schema.tables.iter_mut() { + // skip the table we just renamed + if normalize_ident(tname) == normalized_to { + continue; + } + if let Table::BTree(ref mut child_btree_arc) = Arc::make_mut(t_arc) { + let child_btree = Arc::make_mut(child_btree_arc); + for fk_arc in &mut child_btree.foreign_keys { + if normalize_ident(&fk_arc.parent_table) == normalized_from { + let fk = Arc::make_mut(fk_arc); + fk.parent_table = normalized_to.clone(); + } + } + } + } }); state.pc += 1;