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

planner: support foreign key choose parent schema as default in CreateTable stmt (#12548) #12675

Merged
merged 3 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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