此插入在我的数据库上失败-
insert into tig_pairs (pkey, pval, uid) select 'schema-version', '4.0', uid from tig_users where (sha1_user_id = sha1(lower('db-properties')));
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`tigasedb`.`tig_pairs`, CONSTRAINT `tig_pairs_constr_2` FOREIGN KEY (`nid`) REFERENCES `tig_nodes` (`nid`))
表定义在哪里:
create table if not exists tig_pairs (
nid int unsigned,
uid int unsigned NOT NULL,
pkey varchar(255) NOT NULL,
pval mediumtext,
PRIMARY KEY (nid, pkey), -- ***
key pkey (pkey),
key uid (uid),
key nid (nid),
constraint tig_pairs_constr_1 foreign key (uid) references tig_users (uid),
constraint tig_pairs_constr_2 foreign key (nid) references tig_nodes (nid)
)
ENGINE=InnoDB default character set utf8 ROW_FORMAT=DYNAMIC;
和
create table if not exists tig_nodes (
nid int unsigned NOT NULL auto_increment,
parent_nid int unsigned,
uid int unsigned NOT NULL,
node varchar(255) NOT NULL,
primary key (nid),
unique key tnode (parent_nid, uid, node),
key node (node),
key uid (uid),
key parent_nid (parent_nid),
constraint tig_nodes_constr foreign key (uid) references tig_users (uid)
)
ENGINE=InnoDB default character set utf8 ROW_FORMAT=DYNAMIC;
该行PRIMARY KEY (nid, pkey), -- ***
被省略,然后我的查询就可以通过了。该主键和令人不安的外键约束之间是否存在冲突?我怎样才能避免它?主键必须留在那里:)
谢谢!
编辑:通过在一行上更改 tig_pairs 定义来消除错误:
nid int unsigned NOT NULL auto_increment,