diff --git a/ddl/db_test.go b/ddl/db_test.go index f0087a91e9183..14a3bffde7dfe 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -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") diff --git a/ddl/ddl.go b/ddl/ddl.go index e7154f96c6531..83e53a3bdbcde 100644 --- a/ddl/ddl.go +++ b/ddl/ddl.go @@ -97,7 +97,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") @@ -579,7 +579,7 @@ const ( codeTooLongIdent = 1059 codeDupKeyName = 1061 codeTooLongKey = 1071 - codeKeyColumnDoesNotExits = 1072 + codeKeyColumnDoesNotExits = mysql.ErrKeyColumnDoesNotExits codeIncorrectPrefixKey = 1089 codeCantRemoveAllFields = 1090 codeCantDropFieldOrKey = 1091 diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 3bb2807bf3afa..2709e3174ecd4 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -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 { @@ -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 {