From 67d8fbf23eee71f8b6ddc4221e7bf8bdc5fdad9f Mon Sep 17 00:00:00 2001 From: pingcap-github-bot Date: Fri, 18 Oct 2019 17:40:50 +0800 Subject: [PATCH] planner: support foreign key choose parent schema as default in CreateTable stmt (#12548) (#12675) --- ddl/db_integration_test.go | 11 +++++++++++ planner/core/preprocess.go | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index 52fd0f40a208f..7353e57abf6f8 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -1992,3 +1992,14 @@ func (s *testIntegrationSuite4) TestDropAutoIncrementIndex(c *C) { dropIndexSQL = "alter table t1 drop index a" assertErrorCode(c, tk, dropIndexSQL, mysql.ErrWrongAutoKey) } + +func (s *testIntegrationSuite3) TestParserIssue284(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("create table test.t_parser_issue_284(c1 int not null primary key)") + _, err := tk.Exec("create table test.t_parser_issue_284_2(id int not null primary key, c1 int not null, constraint foreign key (c1) references t_parser_issue_284(c1))") + c.Assert(err, IsNil) + + tk.MustExec("drop table test.t_parser_issue_284") + tk.MustExec("drop table test.t_parser_issue_284_2") +} diff --git a/planner/core/preprocess.go b/planner/core/preprocess.go index 662c9f74df11b..3ef613bb48995 100644 --- a/planner/core/preprocess.go +++ b/planner/core/preprocess.go @@ -85,6 +85,7 @@ func (p *preprocessor) Enter(in ast.Node) (out ast.Node, skipChildren bool) { switch node := in.(type) { case *ast.CreateTableStmt: p.flag |= inCreateOrDropTable + p.resolveCreateTableStmt(node) p.checkCreateTableGrammar(node) case *ast.CreateViewStmt: p.flag |= inCreateOrDropTable @@ -738,6 +739,14 @@ func (p *preprocessor) resolveShowStmt(node *ast.ShowStmt) { } } +func (p *preprocessor) resolveCreateTableStmt(node *ast.CreateTableStmt) { + for _, val := range node.Constraints { + if val.Refer != nil && val.Refer.Table.Schema.String() == "" { + val.Refer.Table.Schema = node.Table.Schema + } + } +} + func (p *preprocessor) resolveAlterTableStmt(node *ast.AlterTableStmt) { for _, spec := range node.Specs { if spec.Tp == ast.AlterTableRenameTable {