错误上传db到mysql

时间:2014-12-25 15:12:30

标签: mysql key

-- Constraints for table `variation`
--
ALTER TABLE `variation`
  ADD FOREIGN KEY (`parent`) REFERENCES `variation` (`id`),
  ADD FOREIGN KEY (`scale`) REFERENCES `scale` (`id`),
  ADD FOREIGN KEY (`user`) REFERENCES `users` (`user_id`);

--

错误

  

ERROR 1452(23000)第277行:无法添加或更新子行:a   外键约束失败(getsy#sql-f44_2a,CONSTRAINT   #sql-f44_2a_ibfk_2外键(scale)参考scaleid))

此表变体

CREATE TABLE IF NOT EXISTS `variation` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `parent` bigint(20) DEFAULT NULL,
  `scale` bigint(20) DEFAULT NULL,
  `value` varchar(255) NOT NULL,
  `user` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `parent` (`parent`,`scale`,`value`,`user`),
  KEY `scale` (`scale`),
  KEY `user` (`user`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=101 ;

帮帮我......

感谢

1 个答案:

答案 0 :(得分:0)

variation表格列scale中的内容似乎包含表scaleid中缺少的值。

您可以使用以下查询检查缺失值:

SELECT DISTINCT a.scale FROM 
   variation AS a LEFT JOIN scale AS b ON a.scale = b.id 
WHERE b.id IS NULL;