SQL Error: 1452, SQLState: 23000

今天遇到调代码的时候遇到一个问题,就是我删除了数据库的所有的记录之后,然后通过前台页面的注册页面进行注册 ,

但是报出SQL Error: 1452, SQLState: 23000;    其他错误如下:

 [tomcat-http--5] WARN  org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1452, SQLState: 23000
2017-03-31 12:27:35 [tomcat-http--5] WARN  org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1452, SQLState: 23000
2017-03-31 12:27:35 [tomcat-http--5] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Cannot add or update a child row: a foreign key constraint fails (`chatwork`.`user_role`, CONSTRAINT `FK_02dcca8f72554a62bdec8067a49` FOREIGN KEY (`role_id`) REFERENCES `role` (`id`))
2017-03-31 12:27:35 [tomcat-http--5] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Cannot add or update a child row: a foreign key constraint fails (`chatwork`.`user_role`, CONSTRAINT `FK_02dcca8f72554a62bdec8067a49` FOREIGN KEY (`role_id`) REFERENCES `role` (`id`))
2017-03-31 12:27:35 [tomcat-http--5] ERROR org.hibernate.engine.jdbc.batch.internal.BatchingBatch - HHH000315: Exception executing batch [could not perform addBatch]
2017-03-31 12:27:35 [tomcat-http--5] ERROR org.hibernate.engine.jdbc.batch.internal.BatchingBatch - HHH000315: Exception executing batch [could not perform addBatch]
org.springframework.dao.DataIntegrityViolationException: could not perform addBatch; SQL [insert into user_role (nickName, role_id) values (?, ?)]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not perform addBatch

出现这个错误是因为外键为空,所以报错。因为是测试数据库,里面没有重要的东西所以我就把数据库删了 然后重启项目,创建表。

 

 

解决办法:  首先检查自己的表的关系,可以在后台通过set方法设置 ,或者直接先在数据库中添加这个外键;还有一点就是不要随便删库!!!

你可能感兴趣的:(【,数据库,篇】)