3
我已經看過其他人關於此主題的問題,但似乎無法找到我的錯誤來自哪裏。任何幫助將不勝感激。MySQL錯誤1452-無法添加或更新子行:外鍵約束失敗
CREATE TABLE stocks (
id INT AUTO_INCREMENT NOT NULL,
user_id INT(11) UNSIGNED NOT NULL,
ticker VARCHAR(20) NOT NULL,
name VARCHAR(20),
rating INT(11),
position ENUM("strong buy", "buy", "sell", "strong sell", "neutral"),
next_look DATE,
privacy ENUM("public", "private"),
PRIMARY KEY(id),
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `stocks_tags` (
`stock_id` INT NOT NULL,
`tag_id` INT NOT NULL,
PRIMARY KEY (`stock_id`,`tag_id`),
KEY `fk_stock_tag` (`tag_id`),
KEY `fk_tag_stock` (`stock_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `stocks_tags`
ADD CONSTRAINT `fk_stock_tag` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `fk_tag_stock` FOREIGN KEY (`stock_id`) REFERENCES `stocks` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
CREATE TABLE tags(
id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
tags VARCHAR(30),
UNIQUE(tags)
)
ENGINE=INNODB DEFAULT CHARSET=utf8;
而我得到的錯誤:
Database_Exception [ 1452 ]: Cannot add or update a child row: a foreign key constraint
fails (`ddmachine`.`stocks_tags`, CONSTRAINT `fk_stock_tag` FOREIGN KEY (`tag_id`) REFERENCES
`tags` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) [ INSERT INTO `stocks_tags` (`stock_id`,
`tag_id`) VALUES (19, 'cash') ]
我沒看到別人也有類似的問題,我能想到的,可能有助於發現問題,我包括儘可能多基於他們的枚舉列,但不認爲就是這樣。
是的,我一定是在電腦前盯着今天太久!多可笑。我認爲這很複雜,因爲我使用的是ORM,所以我的模型必須是不正確的。 – dscher 2010-04-24 04:56:05