刷新数据库总是报错:不能增加或者更新约束, Cannot add or update a child row: a foreign key constraint fails

$2.run(QueuedThreadPool.java:590)
	at java.lang.Thread.run(Unknown Source)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
 Cannot add or update a child row: a foreign key constraint fails (`manager2`.`#sql-7d0_26`, CONSTRAINT `FKgg6g1h63qg29is5ypae70elgt` FOREIGN KEY (`model_id`) REFERENCES `saas_model` (`id`))
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
 

这是因为数据冗余造成不能正常更行数据,删去这些数据就行了,认真看报错信息,给的提示也挺完整的,比如:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
 Cannot add or update a child row: a foreign key constraint fails 
(`manager2`.`#sql-7d0_21`, CONSTRAINT `FK47i0hrv2j92ffjagi98vf3si9` FOREIGN KEY (`parent_id`) REFERENCES `saas_element` (`id`))
	at 
就是table saas_element 中id 和parent_id  冲突,具体是,parent_id ,已经删去,但是仍然存在parent为xxx的数据;

select * from saas_element a where a.parent_id not in  (select id from saas_element ) 

-----------------over--------------

下面是我自己的删除数据

select * from saas_element a where a.parent_id not in  (select id from saas_element )  
父id 不在id中;即有父但是父已经不存在的,所有数据
select * from saas_modelfield a where a.model_id not in  (select id from saas_model )  
在 modelfield 表中,查model_id 已经不存在但是还有数据的这些信息

select * from saas_taglibimport a where a.layout_id not in  (select id from saas_layout a where a.model_id not in  (select id from saas_model )  )  
layout_element
select * from saas_layout_element a where a.Layout_id not in  (select id from saas_layout a where a.model_id not in  (select id from saas_model )  )  
delet table ,tagcatagroy,tagimport 

select * from saas_layout a where a.model_id not in  (select id from saas_model )  

select * from saas_taginfoproperty a where a.taginfo_id not in  (select id from saas_taginfo )  
select * from saas_layout a where a.prevVersion_id not in  (select id from saas_Layout )  
//remove taglibimport retrain of layout
select * from saas_layout a where a.model_id not in  (select id from saas_model )
select * from saas_layout a where a.prevVersion_id not in  (select id from saas_Layout )

//menu ,pageid

select * from saas_page a where a.layout_id not in  (select id from saas_layout )  
//删去能删的数据剩下page Id 88;in saas_menu 
select * from saas_page a where a.layout_id not in  (select id from saas_layout )

select * from saas_layout_element a where a.layout_id not in  (select id from saas_layout )

//menu_page page,
select * from saas_menu_page a where a.relevantPages_id not in  (select id from saas_page )
//e
select * from saas_taglibimport a where a.layout_id not in  (select id from saas_layout )  

http://blog.csdn.net/flymoringbird/article/details/52468966
select * from INFORMATION_SCHEMA.KEY_COLUMN_USAGE //这个是查询出所有的主键和外键


发布了28 篇原创文章 · 获赞 5 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/java_utf8/article/details/65448852