我真的无法得到线索,因为一切似乎都没问题。这是我正在尝试的命令:
ALTER TABLE dashboard ADD CONSTRAINT FOREIGN KEY (app_name, app_parent, client)
REFERENCES apps(app_name, app_parent, client) ON DELETE CASCADE ON UPDATE CASCADE;
这是我得到的错误:
ERROR 1215 (HY000): Cannot add foreign key constraint
使用FK的显式名称没有帮助。这是引用的表:
mysql> SHOW COLUMNS IN apps;
+------------+------------------+------+-----+------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+------------+-------+
| app_name | varchar(20) | NO | PRI | | |
| app_parent | varchar(20) | NO | PRI | | |
| client | varchar(12) | NO | PRI | NULL | |
| order_idx | int(10) unsigned | YES | | 100 | |
...
这是引用表:
mysql> SHOW COLUMNS IN dashboard;
+------------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------------------+------+-----+---------+-------+
| app_name | varchar(20) | NO | | | |
| app_parent | varchar(20) | NO | | | |
| client | varchar(12) | NO | | NULL | |
| widget | varchar(20) | YES | | NULL | |
...
两个表都有InnoDB作为引擎。这是我从SHOW ENGINE InnoDB STATUS
得到的(相当简洁的)线索:
LATEST FOREIGN KEY ERROR
------------------------
2014-06-20 11:49:57 10ec Error in foreign key constraint of table the_db/#sql-128c_2:
FOREIGN KEY (app_name, app_parent, client)
REFERENCES apps(app_name, app_parent, client) ON DELETE CASCADE ON UPDATE CASCADE:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html
for correct foreign key definition.
在apps
找不到索引是什么意思?我正在引用主键!而且类型明显相同。
编辑:这是针对应用的CREATE命令:
CREATE TABLE `apps` (
`app_name` varchar(20) NOT NULL DEFAULT '',
`app_parent` varchar(20) NOT NULL DEFAULT '',
`client` varchar(12) NOT NULL,
`order_idx` int(10) unsigned DEFAULT '100',
...,
PRIMARY KEY (`app_name`,`app_parent`,`client`),
KEY `client` (`client`),
CONSTRAINT `apps_ibfk_1` FOREIGN KEY (`client`) REFERENCES `clients` (`name`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `apps_ibfk_2` FOREIGN KEY (`app_name`, `app_parent`) REFERENCES `app_list` (`name`, `parent`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
我必须补充一点,我实际上设法在另一个表的相同列上创建了一个外键:
mysql> SHOW COLUMNS IN user_apps;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| app_name | varchar(20) | NO | PRI | | |
| app_parent | varchar(20) | NO | PRI | | |
| username | varchar(20) | NO | PRI | | |
| client | varchar(12) | YES | | NULL | |
...
此处,app_name
,app_parent
和client
引用了apps
中相同的命名列。
答案 0 :(得分:1)
这可能是最错误的错误:问题是默认字符集,即应用程序的latin1和仪表板的utf8。
这解决了我的问题:
ALTER TABLE dashboard CONVERT TO CHARACTER SET latin1;
答案 1 :(得分:0)
你最好发布
的输出show create table apps
我不确定引用的父列是否已编入索引
但是为了让他们有资格从其他子表中引用,他们必须 INDEXed
根据foreign key
约束的文档:
REFERENCES tbl_name(index_col_name,...)
在INDEX
表格的app_name, app_parent, client
列定义apps
。
如果尚未定义,则使用以下命令:
ALTER TABLE apps
ADD INDEX ix_app_name( app_name )
, ADD INDEX ix_app_parent( app_parent )
, ADD INDEX ix_app_client( client )
其他主要限制因素是,
ENGINE
类型必须相同。请参阅:
[CONSTRAINT [symbol]] FOREIGN KEY
[index_name](index_col_name,...)
参考文献tbl_name(index_col_name,...)
[ON DELETE reference_option]
[ON UPDATE reference_option]reference_option:
限制| CASCADE | SET NULL |没有行动