Skip to content

Commit

Permalink
planner: support foreign key choose parent schema as default in Creat…
Browse files Browse the repository at this point in the history
…eTable stmt (#12548) (#12675)
  • Loading branch information
sre-bot authored Oct 18, 2019
1 parent 14097d8 commit 67d8fbf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
9 changes: 9 additions & 0 deletions planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 67d8fbf

Please sign in to comment.