Skip to content

Commit 7cd9b9c

Browse files
shlomi-noachdbussink
authored andcommitted
Allowing vitess Online DDL in Vitess to work with foreign keys.
We wish to solve the issue of an Online DDL cut-over. We distinguish two scenarios: migrating a foreign key (FK) _parent_ table, and migrating a FK _child_ table. Since we rename the parent table away, we must iterate its children. Standard MySQL behavior is for children to follow the renamed parent into its new name. In fact, internally to InnoDB, the foreign key relationships just maintain their existing pointer referenced between the related tables. All that MySQL does is to modify the children's `CREATE TABLE` statement to point to the new name. **What we do**: we avoid changing the children's `CREATE TABLE` statement, and we ensure to refresh (reset) the FK relationship, such that the child points to the new parent (new parent assumed the old parent's name). This seems to work well under stress tests. As a reminder: if we are migrating a child table, then we create a shadow table, that will eventually replace the child. If that shadow table references the parent, then in the duration of the migration, and while the shadow table is not in full sync with the original child table, the shadow table may actually cause valid queries running on the parent table to fail. Specifically, a `DELETE FROM parent_table WHERE...` can fail if the shadow table has rows matching the rows deleted on the parent. In this PR we let the shadow table reference the same parent. We tell InnoDB to ignore FK relationships on tables which match internal Vitess names, namely names like: - `_84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl` (Online DDL vreplication shadow table format) - `_vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410` (GC name format) - `_vt_DROP_6ace8bcef73211ea87e9f875a4d24e90_20200915120410` (GC name format) With the new rules, InnoDB treats any FK relationship involving Vitess-internal tables as if we set `FOREIGN_KEY_CHECKS=0`, specific to those tables. Examples: - If `parent_table` is parent, and `_84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl` is child, then - It is possible to `INSERT` any rows to `_84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl`, whether they do or do not match rows in `parent_table` - It is possible to `DELETE` or `UPDATE` rows on `parent_table`, irrespective of whether matching rows do or do not exist in `_84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl`. There will not even be a check. - `CASCADE` and `SET NULL` are never attempted on `_84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl` - If `_84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl` is the parent, and `child_table` is child, then - It is possible to `DELETE` or `UPDATE` any rows on `_84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl`, regardless of whether matching rows do or do not exist in `child_table` - It is possible to `DROP TABLE _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl` A use case might be: ```sql SET rename_table_preserve_foreign_key=1; rename table a to b, b to c, c to a; SET rename_table_preserve_foreign_key=0; ``` This makes it simple to verify whether a MySQL server supports this feature, as in: ```sql > show variables like 'rename_table_preserve_foreign_key'; +-----------------------------------+-------+ | Variable_name | Value | +-----------------------------------+-------+ | rename_table_preserve_foreign_key | OFF | +-----------------------------------+-------+ ``` A server which does not support this variable will just return an empty result set, and without any error. Signed-off-by: Dirkjan Bussink <[email protected]>
1 parent 5237292 commit 7cd9b9c

16 files changed

+991
-4
lines changed
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
drop table if exists t0,t1,t2,t3,t4;
2+
drop table if exists t0,t5,t6,t7,t8,t9,t1_1,t1_2,t9_1,t9_2;
3+
drop table if exists _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl, _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410, child_table, child_table_old, child_table_new;
4+
drop table if exists parent_table, parent_table_old, parent_table_new;
5+
SET FOREIGN_KEY_CHECKS=1;
6+
CREATE TABLE parent_table (
7+
id INT NOT NULL,
8+
PRIMARY KEY (id)
9+
);
10+
CREATE TABLE child_table (
11+
id INT NOT NULL,
12+
parent_id INT,
13+
PRIMARY KEY (id),
14+
KEY parent_id_idx (parent_id),
15+
CONSTRAINT `child_parent_fk` FOREIGN KEY (parent_id) REFERENCES parent_table(id) ON DELETE NO ACTION
16+
);
17+
insert into parent_table values (1), (2);
18+
insert into child_table values (10, 1);
19+
insert into child_table values (11, 1);
20+
insert into child_table values (12, 2);
21+
insert into child_table values (13, 2);
22+
CREATE TABLE _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl (
23+
id INT NOT NULL,
24+
parent_id INT,
25+
PRIMARY KEY (id),
26+
KEY parent_id_idx (parent_id),
27+
CONSTRAINT `vrepl_child_parent_fk` FOREIGN KEY (parent_id) REFERENCES parent_table(id) ON DELETE NO ACTION
28+
);
29+
insert into _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl select * from child_table;
30+
CREATE TABLE _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410 (
31+
id INT NOT NULL,
32+
parent_id INT,
33+
PRIMARY KEY (id),
34+
KEY parent_id_idx (parent_id),
35+
CONSTRAINT `hold_child_parent_fk` FOREIGN KEY (parent_id) REFERENCES parent_table(id) ON DELETE NO ACTION
36+
);
37+
insert into _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410 select * from child_table;
38+
insert into child_table values (100, 100);
39+
ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`child_table`, CONSTRAINT `child_parent_fk` FOREIGN KEY (`parent_id`) REFERENCES `parent_table` (`id`))
40+
insert into _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl values (101, 101);
41+
insert into _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410 values (102, 102);
42+
delete from parent_table where id=1;
43+
ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`child_table`, CONSTRAINT `child_parent_fk` FOREIGN KEY (`parent_id`) REFERENCES `parent_table` (`id`))
44+
delete from child_table;
45+
delete from parent_table where id=1;
46+
drop table parent_table;
47+
ERROR HY000: Cannot drop table 'parent_table' referenced by a foreign key constraint 'child_parent_fk' on table 'child_table'.
48+
drop table child_table;
49+
drop table parent_table;
50+
drop table _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl;
51+
drop table _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410;
52+
CREATE TABLE parent_table (
53+
id INT NOT NULL,
54+
PRIMARY KEY (id)
55+
);
56+
CREATE TABLE child_table (
57+
id INT NOT NULL,
58+
parent_id INT,
59+
PRIMARY KEY (id),
60+
KEY parent_id_idx (parent_id),
61+
CONSTRAINT `child_parent_fk` FOREIGN KEY (parent_id) REFERENCES parent_table(id) ON DELETE CASCADE
62+
);
63+
insert into parent_table values (1), (2);
64+
insert into child_table values (10, 1);
65+
insert into child_table values (11, 1);
66+
insert into child_table values (12, 2);
67+
insert into child_table values (13, 2);
68+
CREATE TABLE _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl (
69+
id INT NOT NULL,
70+
parent_id INT,
71+
PRIMARY KEY (id),
72+
KEY parent_id_idx (parent_id),
73+
CONSTRAINT `vrepl_child_parent_fk` FOREIGN KEY (parent_id) REFERENCES parent_table(id) ON DELETE CASCADE
74+
);
75+
insert into _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl select * from child_table;
76+
CREATE TABLE _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410 (
77+
id INT NOT NULL,
78+
parent_id INT,
79+
PRIMARY KEY (id),
80+
KEY parent_id_idx (parent_id),
81+
CONSTRAINT `hold_child_parent_fk` FOREIGN KEY (parent_id) REFERENCES parent_table(id) ON DELETE CASCADE
82+
);
83+
insert into _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410 select * from child_table;
84+
insert into child_table values (200, 200);
85+
ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`child_table`, CONSTRAINT `child_parent_fk` FOREIGN KEY (`parent_id`) REFERENCES `parent_table` (`id`) ON DELETE CASCADE)
86+
insert into _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl values (201, 201);
87+
insert into _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410 values (202, 202);
88+
delete from parent_table where id=1;
89+
select * from child_table;
90+
id parent_id
91+
12 2
92+
13 2
93+
select * from _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl;
94+
id parent_id
95+
10 1
96+
11 1
97+
12 2
98+
13 2
99+
201 201
100+
select * from _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410;
101+
id parent_id
102+
10 1
103+
11 1
104+
12 2
105+
13 2
106+
202 202
107+
drop table parent_table;
108+
ERROR HY000: Cannot drop table 'parent_table' referenced by a foreign key constraint 'child_parent_fk' on table 'child_table'.
109+
drop table child_table;
110+
drop table parent_table;
111+
drop table _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl;
112+
drop table _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410;
113+
CREATE TABLE parent_table (
114+
id INT NOT NULL,
115+
PRIMARY KEY (id)
116+
);
117+
CREATE TABLE child_table (
118+
id INT NOT NULL,
119+
parent_id INT,
120+
PRIMARY KEY (id),
121+
KEY parent_id_idx (parent_id),
122+
CONSTRAINT `child_parent_fk` FOREIGN KEY (parent_id) REFERENCES parent_table(id) ON DELETE SET NULL
123+
);
124+
insert into parent_table values (1), (2);
125+
insert into child_table values (10, 1);
126+
insert into child_table values (11, 1);
127+
insert into child_table values (12, 2);
128+
insert into child_table values (13, 2);
129+
CREATE TABLE _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl (
130+
id INT NOT NULL,
131+
parent_id INT,
132+
PRIMARY KEY (id),
133+
KEY parent_id_idx (parent_id),
134+
CONSTRAINT `vrepl_child_parent_fk` FOREIGN KEY (parent_id) REFERENCES parent_table(id) ON DELETE SET NULL
135+
);
136+
insert into _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl select * from child_table;
137+
CREATE TABLE _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410 (
138+
id INT NOT NULL,
139+
parent_id INT,
140+
PRIMARY KEY (id),
141+
KEY parent_id_idx (parent_id),
142+
CONSTRAINT `hold_child_parent_fk` FOREIGN KEY (parent_id) REFERENCES parent_table(id) ON DELETE SET NULL
143+
);
144+
insert into _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410 select * from child_table;
145+
insert into child_table values (300, 300);
146+
ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`child_table`, CONSTRAINT `child_parent_fk` FOREIGN KEY (`parent_id`) REFERENCES `parent_table` (`id`) ON DELETE SET NULL)
147+
insert into _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl values (301, 301);
148+
insert into _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410 values (302, 302);
149+
delete from parent_table where id=1;
150+
select * from child_table;
151+
id parent_id
152+
10 NULL
153+
11 NULL
154+
12 2
155+
13 2
156+
select * from _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl;
157+
id parent_id
158+
10 1
159+
11 1
160+
12 2
161+
13 2
162+
301 301
163+
select * from _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410;
164+
id parent_id
165+
10 1
166+
11 1
167+
12 2
168+
13 2
169+
302 302
170+
drop table parent_table;
171+
ERROR HY000: Cannot drop table 'parent_table' referenced by a foreign key constraint 'child_parent_fk' on table 'child_table'.
172+
drop table child_table;
173+
drop table parent_table;
174+
drop table _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl;
175+
drop table _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410;
176+
CREATE TABLE _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl (
177+
id INT NOT NULL,
178+
PRIMARY KEY (id)
179+
);
180+
CREATE TABLE child_table (
181+
id INT NOT NULL,
182+
parent_id INT,
183+
PRIMARY KEY (id),
184+
KEY parent_id_idx (parent_id),
185+
CONSTRAINT `child_parent_fk` FOREIGN KEY (parent_id) REFERENCES _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl(id) ON DELETE RESTRICT
186+
);
187+
insert into _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl values (1), (2);
188+
insert into child_table values (10, 1);
189+
insert into child_table values (11, 1);
190+
insert into child_table values (12, 2);
191+
insert into child_table values (13, 2);
192+
insert into child_table values (400, 400);
193+
delete from _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl where id=1;
194+
select * from child_table;
195+
id parent_id
196+
10 1
197+
11 1
198+
12 2
199+
13 2
200+
400 400
201+
drop table _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl;
202+
drop table if exists _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl, _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410, child_table, child_table_old, child_table_new;
203+
drop table if exists parent_table, parent_table_old, parent_table_new;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
drop table if exists t0,t1,t2,t3,t4;
2+
drop table if exists t0,t5,t6,t7,t8,t9,t1_1,t1_2,t9_1,t9_2;
3+
drop table if exists _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl, _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410, child_table, child_table_old, child_table_new;
4+
drop table if exists parent_table, parent_table_old, parent_table_new;
5+
CREATE TABLE parent_table (
6+
id INT NOT NULL,
7+
PRIMARY KEY (id)
8+
);
9+
CREATE TABLE child_table (
10+
id INT NOT NULL,
11+
parent_id INT,
12+
PRIMARY KEY (id),
13+
KEY parent_id_idx (parent_id),
14+
CONSTRAINT `child_parent_fk` FOREIGN KEY (parent_id) REFERENCES parent_table(id) ON DELETE NO ACTION
15+
);
16+
insert into parent_table values (1), (2);
17+
insert into child_table values (10, 1);
18+
insert into child_table values (11, 1);
19+
insert into child_table values (12, 2);
20+
insert into child_table values (13, 2);
21+
CREATE TABLE parent_table_new (
22+
id INT NOT NULL,
23+
hint INT NOT NULL DEFAULT 0,
24+
PRIMARY KEY (id)
25+
);
26+
insert into parent_table_new values (1, 7), (2, 7);
27+
SET FOREIGN_KEY_CHECKS=1;
28+
rename table parent_table to parent_table_old, parent_table_new to parent_table;
29+
show create table child_table;
30+
Table Create Table
31+
child_table CREATE TABLE `child_table` (
32+
`id` int NOT NULL,
33+
`parent_id` int DEFAULT NULL,
34+
PRIMARY KEY (`id`),
35+
KEY `parent_id_idx` (`parent_id`),
36+
CONSTRAINT `child_parent_fk` FOREIGN KEY (`parent_id`) REFERENCES `parent_table_old` (`id`)
37+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
38+
delete from parent_table where id=1;
39+
delete from parent_table_old where id=1;
40+
ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`child_table`, CONSTRAINT `child_parent_fk` FOREIGN KEY (`parent_id`) REFERENCES `parent_table_old` (`id`))
41+
drop table if exists _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl, _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410, child_table, child_table_old, child_table_new;
42+
drop table if exists parent_table, parent_table_old, parent_table_new;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
drop table if exists t0,t1,t2,t3,t4;
2+
drop table if exists t0,t5,t6,t7,t8,t9,t1_1,t1_2,t9_1,t9_2;
3+
drop table if exists _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl, _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410, child_table, child_table_old, child_table_new;
4+
drop table if exists parent_table, parent_table_old, parent_table_new;
5+
CREATE TABLE parent_table (
6+
id INT NOT NULL,
7+
PRIMARY KEY (id)
8+
);
9+
CREATE TABLE child_table (
10+
id INT NOT NULL,
11+
parent_id INT,
12+
PRIMARY KEY (id),
13+
KEY parent_id_idx (parent_id),
14+
CONSTRAINT `child_parent_fk` FOREIGN KEY (parent_id) REFERENCES parent_table(id) ON DELETE NO ACTION
15+
);
16+
insert into parent_table values (1), (2);
17+
insert into child_table values (10, 1);
18+
insert into child_table values (11, 1);
19+
insert into child_table values (12, 2);
20+
insert into child_table values (13, 2);
21+
CREATE TABLE _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl (
22+
id INT NOT NULL,
23+
parent_id INT,
24+
PRIMARY KEY (id),
25+
KEY parent_id_idx (parent_id),
26+
CONSTRAINT `new_child_parent_fk` FOREIGN KEY (parent_id) REFERENCES parent_table(id) ON DELETE NO ACTION
27+
);
28+
insert into _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl select * from child_table;
29+
SET FOREIGN_KEY_CHECKS=1;
30+
SET rename_table_preserve_foreign_key=1;
31+
rename table child_table to _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410, _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl to child_table, _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410 to _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl;
32+
SET rename_table_preserve_foreign_key=0;
33+
show create table child_table;
34+
Table Create Table
35+
child_table CREATE TABLE `child_table` (
36+
`id` int NOT NULL,
37+
`parent_id` int DEFAULT NULL,
38+
PRIMARY KEY (`id`),
39+
KEY `parent_id_idx` (`parent_id`),
40+
CONSTRAINT `new_child_parent_fk` FOREIGN KEY (`parent_id`) REFERENCES `parent_table` (`id`)
41+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
42+
select * from child_table;
43+
id parent_id
44+
10 1
45+
11 1
46+
12 2
47+
13 2
48+
show create table _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl;
49+
Table Create Table
50+
_84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl CREATE TABLE `_84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl` (
51+
`id` int NOT NULL,
52+
`parent_id` int DEFAULT NULL,
53+
PRIMARY KEY (`id`),
54+
KEY `parent_id_idx` (`parent_id`),
55+
CONSTRAINT `child_parent_fk` FOREIGN KEY (`parent_id`) REFERENCES `parent_table` (`id`)
56+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
57+
select * from _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl;
58+
id parent_id
59+
10 1
60+
11 1
61+
12 2
62+
13 2
63+
delete from parent_table where id=1;
64+
ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`child_table`, CONSTRAINT `new_child_parent_fk` FOREIGN KEY (`parent_id`) REFERENCES `parent_table` (`id`))
65+
delete from child_table;
66+
delete from parent_table where id=1;
67+
drop table if exists _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl, _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410, child_table, child_table_old, child_table_new;
68+
drop table if exists parent_table, parent_table_old, parent_table_new;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
drop table if exists t0,t1,t2,t3,t4;
2+
drop table if exists t0,t5,t6,t7,t8,t9,t1_1,t1_2,t9_1,t9_2;
3+
drop table if exists _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl, _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410, child_table, child_table_old, child_table_new;
4+
drop table if exists parent_table, parent_table_old, parent_table_new;
5+
CREATE TABLE parent_table (
6+
id INT NOT NULL,
7+
PRIMARY KEY (id)
8+
);
9+
CREATE TABLE child_table (
10+
id INT NOT NULL,
11+
parent_id INT,
12+
PRIMARY KEY (id),
13+
KEY parent_id_idx (parent_id),
14+
CONSTRAINT `child_parent_fk` FOREIGN KEY (parent_id) REFERENCES parent_table(id) ON DELETE NO ACTION
15+
);
16+
insert into parent_table values (1), (2);
17+
insert into child_table values (10, 1);
18+
insert into child_table values (11, 1);
19+
insert into child_table values (12, 2);
20+
insert into child_table values (13, 2);
21+
CREATE TABLE parent_table_new (
22+
id INT NOT NULL,
23+
hint INT NOT NULL DEFAULT 0,
24+
PRIMARY KEY (id)
25+
);
26+
insert into parent_table_new values (1, 7), (2, 7), (55, 7);
27+
SET FOREIGN_KEY_CHECKS=1;
28+
SET rename_table_preserve_foreign_key=1;
29+
rename table parent_table to parent_table_old, parent_table_new to parent_table;
30+
SET rename_table_preserve_foreign_key=0;
31+
show create table child_table;
32+
Table Create Table
33+
child_table CREATE TABLE `child_table` (
34+
`id` int NOT NULL,
35+
`parent_id` int DEFAULT NULL,
36+
PRIMARY KEY (`id`),
37+
KEY `parent_id_idx` (`parent_id`),
38+
CONSTRAINT `child_parent_fk` FOREIGN KEY (`parent_id`) REFERENCES `parent_table` (`id`)
39+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
40+
select id, hint from parent_table;
41+
id hint
42+
1 7
43+
2 7
44+
55 7
45+
delete from parent_table where id=1;
46+
ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`child_table`, CONSTRAINT `child_parent_fk` FOREIGN KEY (`parent_id`) REFERENCES `parent_table` (`id`))
47+
delete from parent_table_old where id=1;
48+
insert into child_table values (14, 55);
49+
alter table child_table force;
50+
select id, hint from parent_table;
51+
id hint
52+
1 7
53+
2 7
54+
55 7
55+
delete from parent_table_old;
56+
delete from parent_table where id=2;
57+
ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`child_table`, CONSTRAINT `child_parent_fk` FOREIGN KEY (`parent_id`) REFERENCES `parent_table` (`id`))
58+
drop table if exists _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl, _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410, child_table, child_table_old, child_table_new;
59+
drop table if exists parent_table, parent_table_old, parent_table_new;

0 commit comments

Comments
 (0)