Skip to content

Commit 94666a2

Browse files
authored
infoschema_v2: remove schemaTableInfos for foreign key (#55624)
close #55623
1 parent 861607f commit 94666a2

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

pkg/ddl/foreign_key.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -589,15 +589,15 @@ func checkDatabaseHasForeignKeyReferred(ctx context.Context, is infoschema.InfoS
589589
if !fkCheck {
590590
return nil
591591
}
592-
tables, err := is.SchemaTableInfos(ctx, schema)
592+
tableNameInfos, err := is.SchemaSimpleTableInfos(ctx, schema)
593593
if err != nil {
594594
return errors.Trace(err)
595595
}
596-
tableNames := make([]ast.Ident, len(tables))
597-
for i := range tables {
598-
tableNames[i] = ast.Ident{Schema: schema, Name: tables[i].Name}
596+
tableNames := make([]ast.Ident, len(tableNameInfos))
597+
for i := range tableNameInfos {
598+
tableNames[i] = ast.Ident{Schema: schema, Name: tableNameInfos[i].Name}
599599
}
600-
for _, tbl := range tables {
600+
for _, tbl := range tableNameInfos {
601601
if referredFK := checkTableHasForeignKeyReferred(is, schema.L, tbl.Name.L, tableNames, fkCheck); referredFK != nil {
602602
return errors.Trace(dbterror.ErrForeignKeyCannotDropParent.GenWithStackByArgs(tbl.Name, referredFK.ChildFKName, referredFK.ChildTable))
603603
}

tests/integrationtest/r/ddl/foreign_key.result

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,21 @@ select * from t2;
206206
id b
207207
2 2
208208
admin check table t1,t2;
209+
create database test_db_1;
210+
create database test_db_2;
211+
create database test_db_3;
212+
use test_db_1;
213+
create table t1 (id int primary key);
214+
use test_db_2;
215+
create table t2 (id int primary key, b int, foreign key (b) references test_db_1.t1(id));
216+
use test_db_3;
217+
create table t3 (id int primary key);
218+
drop table test_db_1.t1;
219+
Error 3730 (HY000): Cannot drop table 't1' referenced by a foreign key constraint 'fk_1' on table 't2'.
220+
drop database test_db_1;
221+
Error 3730 (HY000): Cannot drop table 't1' referenced by a foreign key constraint 'fk_1' on table 't2'.
222+
drop table test_db_3.t3;
223+
drop database test_db_3;
224+
drop database test_db_2;
225+
drop database test_db_1;
209226
set @@foreign_key_checks=default;

tests/integrationtest/t/ddl/foreign_key.test

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,24 @@ select * from t1;
213213
select * from t2;
214214
admin check table t1,t2;
215215

216+
# TestCheckFKDatabaseRefWithSchemaSimpleTableInfos
217+
create database test_db_1;
218+
create database test_db_2;
219+
create database test_db_3;
220+
use test_db_1;
221+
create table t1 (id int primary key);
222+
use test_db_2;
223+
create table t2 (id int primary key, b int, foreign key (b) references test_db_1.t1(id));
224+
use test_db_3;
225+
create table t3 (id int primary key);
226+
-- error 3730
227+
drop table test_db_1.t1;
228+
-- error 3730
229+
drop database test_db_1;
230+
drop table test_db_3.t3;
231+
drop database test_db_3;
232+
## clean up
233+
drop database test_db_2;
234+
drop database test_db_1;
235+
216236
set @@foreign_key_checks=default;

0 commit comments

Comments
 (0)