Skip to content

Commit

Permalink
ddl: add check when create table with foreign key. (#7885)
Browse files Browse the repository at this point in the history
* ddl: add check when create table with foreign key
  • Loading branch information
crazycs520 authored and ciscoxll committed Oct 17, 2018
1 parent e544882 commit 732233a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
9 changes: 9 additions & 0 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,15 @@ func (s *testDBSuite) TestCreateTable(c *C) {
c.Assert(err, NotNil)
}

func (s *testDBSuite) TestTableForeignKey(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test")
s.tk.MustExec("create table t1 (a int, b int);")
failSQL := "create table t2 (c int, foreign key (a) references t1(a));"
s.testErrorCode(c, failSQL, tmysql.ErrKeyColumnDoesNotExits)
s.tk.MustExec("drop table if exists t1,t2;")
}

func (s *testDBSuite) TestBitDefaultValue(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
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 @@ -585,7 +585,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.FindCol(cols, key.Column.Name.O) == nil {
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

0 comments on commit 732233a

Please sign in to comment.