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 check when create table with foreign key. #7885

Merged
merged 11 commits into from
Oct 17, 2018
4 changes: 2 additions & 2 deletions ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ var (
errIncorrectPrefixKey = terror.ClassDDL.New(codeIncorrectPrefixKey, "Incorrect prefix key; the used key part isn't a string, the used length is longer than the key part, or the storage engine doesn't support unique prefix keys")
errTooLongKey = terror.ClassDDL.New(codeTooLongKey,
fmt.Sprintf("Specified key was too long; max key length is %d bytes", maxPrefixLength))
errKeyColumnDoesNotExits = terror.ClassDDL.New(codeKeyColumnDoesNotExits, "this key column doesn't exist in table")
errKeyColumnDoesNotExits = terror.ClassDDL.New(codeKeyColumnDoesNotExits, mysql.MySQLErrName[mysql.ErrKeyColumnDoesNotExits])
errUnknownTypeLength = terror.ClassDDL.New(codeUnknownTypeLength, "Unknown length for type tp %d")
errUnknownFractionLength = terror.ClassDDL.New(codeUnknownFractionLength, "Unknown Length for type tp %d and fraction %d")
errInvalidJobVersion = terror.ClassDDL.New(codeInvalidJobVersion, "DDL job with version %d greater than current %d")
Expand Down Expand Up @@ -583,7 +583,7 @@ const (
codeTooLongIdent = 1059
codeDupKeyName = 1061
codeTooLongKey = 1071
codeKeyColumnDoesNotExits = 1072
codeKeyColumnDoesNotExits = mysql.ErrKeyColumnDoesNotExits
codeIncorrectPrefixKey = 1089
codeCantRemoveAllFields = 1090
codeCantDropFieldOrKey = 1091
Expand Down
5 changes: 4 additions & 1 deletion ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,9 @@ func buildTableInfo(ctx sessionctx.Context, d *ddl, tableName model.CIStr, cols
fk.RefTable = constr.Refer.Table.Name
fk.State = model.StatePublic
for _, key := range constr.Keys {
if !table.FindColInfo(tbInfo.Columns, key.Column.Name.O) {
Copy link
Contributor

@zimulala zimulala Oct 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use "table.FindCol" directly?

return nil, errKeyColumnDoesNotExits.GenWithStackByArgs(key.Column.Name)
}
fk.Cols = append(fk.Cols, key.Column.Name)
}
for _, key := range constr.Refer.IndexColNames {
Expand All @@ -749,7 +752,7 @@ func buildTableInfo(ctx sessionctx.Context, d *ddl, tableName model.CIStr, cols
for _, key := range constr.Keys {
col = table.FindCol(cols, key.Column.Name.O)
if col == nil {
return nil, errKeyColumnDoesNotExits.GenWithStack("key column %s doesn't exist in table", key.Column.Name)
return nil, errKeyColumnDoesNotExits.GenWithStackByArgs(key.Column.Name)
}
// Virtual columns cannot be used in primary key.
if col.IsGenerated() && !col.GeneratedStored {
Expand Down
9 changes: 9 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,15 @@ func (s *testSuite) TestAdmin(c *C) {
tk.MustExec("admin check table t1;")
}

func (s *testSuite) TestTableForeignKey(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table t1 (a int, b int);")
_, err := tk.Exec("create table t2 (c int, foreign key (a) references t1(a));")
c.Assert(err.Error(), Equals, "[ddl:1072]Key column 'a' doesn't exist in table")
tk.MustExec("drop table if exists t1,t2;")
}

func (s *testSuite) fillData(tk *testkit.TestKit, table string) {
tk.MustExec("use test")
tk.MustExec(fmt.Sprintf("create table %s(id int not null default 1, name varchar(255), PRIMARY KEY(id));", table))
Expand Down
10 changes: 10 additions & 0 deletions table/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ func FindCol(cols []*Column, name string) *Column {
return nil
}

// FindColInfo finds column in cols by name.
func FindColInfo(cols []*model.ColumnInfo, name string) bool {
for _, col := range cols {
if strings.EqualFold(col.Name.O, name) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You pass the name by key.Column.Name.L, but here check with col.Name.O.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return true
}
}
return false
}

// ToColumn converts a *model.ColumnInfo to *Column.
func ToColumn(col *model.ColumnInfo) *Column {
return &Column{
Expand Down