Cross schema rename detaches foreign key
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Bug Report
Please answer these questions before submitting your issue. Thanks!
### 1. Minimal reproduce step (Required)
1. Create a referenced table and a child table in one schema:
```sql
CREATE DATABASE rename_fk_a;
CREATE DATABASE rename_fk_b;
CREATE TABLE rename_fk_b.p (id INT PRIMARY KEY);
CREATE TABLE rename_fk_b.c (
id INT PRIMARY KEY,
pid INT,
CONSTRAINT fk FOREIGN KEY (pid) REFERENCES rename_fk_b.p(id)
);
INSERT INTO rename_fk_b.p VALUES (1);
INSERT INTO rename_fk_b.c VALUES (1,1);
INSERT INTO rename_fk_b.c VALUES (99,99);
-- Control: ERROR 1452
DELETE FROM rename_fk_b.p WHERE id=1;
-- Control: ERROR 1451
```
2. Move only the parent table across schemas while keeping its table name:
```sql
RENAME TABLE rename_fk_b.p TO rename_fk_a.p;
SHOW CREATE TABLE rename_fk_b.c;
```
3. Test both sides of the foreign key after the rename:
```sql
INSERT INTO rename_fk_b.c VALUES (2,2);
DELETE FROM rename_fk_a.p WHERE id=1;
ADMIN CHECK TABLE rename_fk_b.c;
```
4. Optionally create an unrelated table under the old qualified name and observe that the child constraint starts referring to it:
```sql
CREATE TABLE rename_fk_b.p (id INT PRIMARY KEY);
INSERT INTO rename_fk_b.p VALUES (7);
INSERT INTO rename_fk_b.c VALUES (3,3);
```
### 2. What did you expect to see? (Required)
Moving a referenced table from `rename_fk_b.p` to `rename_fk_a.p` should update the child table's foreign-key metadata to the new qualified name. Inserts into the child and deletes from the real parent should remain protected.
### 3. What did you see instead (Required)
The rename succeeds, but `SHOW CREATE TABLE rename_fk_b.c` still shows a reference to the unqualified old name `p`, which resolves as `rename_fk_b.p`. That table no longer exists. An orphan child insert succeeds, deletion of the real parent in `rename_fk_a.p` succeeds, and `ADMIN CHECK TABLE` reports success. If an unrelated table is later created as `rename_fk_b.p`, the foreign key silently attaches to that unrelated table.
### 4. What is your TiDB version? (Required)
```text
Release Version: v8.5.7
Edition: Community
Git Commit Hash: 202b7f47286a1109b5c957401d34c9358d130ae0
Git Branch: HEAD
UTC Build Time: 2026-07-09 23:07:02
GoVersion: go1.25.10
Race Enabled: false
Check Table Before Drop: false
Store: tikv
```
Contributor guide
Assessment
This issue has not been assessed yet.