Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddl: add foreign key compatibility for temporary table #24961

Merged
merged 12 commits into from
Jun 2, 2021
21 changes: 20 additions & 1 deletion ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3011,7 +3011,7 @@ func (s *testDBSuite2) TestTableForeignKey(c *C) {
tk.MustExec("create table t3 (a int, b int);")
failSQL = "alter table t1 add foreign key (c) REFERENCES t3(a);"
tk.MustGetErrCode(failSQL, errno.ErrKeyColumnDoesNotExits)
// test oreign key not match error
// test origin key not match error
failSQL = "alter table t1 add foreign key (a) REFERENCES t3(a, b);"
tk.MustGetErrCode(failSQL, errno.ErrWrongFkDef)
// Test drop column with foreign key.
Expand All @@ -3030,6 +3030,25 @@ func (s *testDBSuite2) TestTableForeignKey(c *C) {
tk.MustExec("drop table if exists t1,t2,t3,t4;")
}

func (s *testDBSuite2) TestTemporaryTableForeignKey(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1;")
tk.MustExec("create table t1 (a int, b int);")
tk.MustExec("drop table if exists t1_tmp;")
tk.MustExec("create global temporary table t1_tmp (a int, b int) on commit delete rows;")
// test add foreign key.
tk.MustExec("drop table if exists t2;")
tk.MustExec("create table t2 (a int, b int);")
_, err := tk.Exec("alter table t1_tmp add foreign key (c) REFERENCES t2(a);")
c.Assert(err.Error(), Equals, ddl.ErrOptOnTemporaryTable.GenWithStackByArgs("foreign key").Error())
// Test drop column with foreign key.
tk.MustExec("create global temporary table t3 (c int,d int,foreign key (d) references t1 (b)) on commit delete rows;")
_, err = tk.Exec("alter table t3 drop foreign key d")
c.Assert(err.Error(), Equals, ddl.ErrOptOnTemporaryTable.GenWithStackByArgs("foreign key").Error())
tk.MustExec("drop table if exists t1,t2,t3,t1_tmp;")
}

func (s *testDBSuite8) TestFKOnGeneratedColumns(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
6 changes: 6 additions & 0 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5264,6 +5264,9 @@ func (d *ddl) CreateForeignKey(ctx sessionctx.Context, ti ast.Ident, fkName mode
if err != nil {
return errors.Trace(infoschema.ErrTableNotExists.GenWithStackByArgs(ti.Schema, ti.Name))
}
if t.Meta().TempTableType != model.TempTableNone {
return ErrOptOnTemporaryTable.GenWithStackByArgs("foreign key")
Howie59 marked this conversation as resolved.
Show resolved Hide resolved
}

fkInfo, err := buildFKInfo(fkName, keys, refer, t.Cols(), t.Meta())
if err != nil {
Expand Down Expand Up @@ -5296,6 +5299,9 @@ func (d *ddl) DropForeignKey(ctx sessionctx.Context, ti ast.Ident, fkName model.
if err != nil {
return errors.Trace(infoschema.ErrTableNotExists.GenWithStackByArgs(ti.Schema, ti.Name))
}
if t.Meta().TempTableType != model.TempTableNone {
Howie59 marked this conversation as resolved.
Show resolved Hide resolved
return ErrOptOnTemporaryTable.GenWithStackByArgs("foreign key")
}

job := &model.Job{
SchemaID: schema.ID,
Expand Down