この挿入は私のデータベースで失敗します-
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), -- ***
が省略され、クエリは問題なく実行されます。その主キーと厄介な外部キー制約の間に競合がありますか?どうすればそれを回避できますか?主キーはそこにとどまる必要があります:)
ありがとう!
編集:1行のtig_pairs定義を変更することでエラーを取り除きました:
nid int unsigned NOT NULL auto_increment,