[已解决]删除失败报错:java.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row.

2023-12-25 18:49:47.680 ERROR 12228 --- [nio-8090-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: 
### Error updating database.  Cause: java.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`memo`.`collection`, CONSTRAINT `fk_collection_note_1` FOREIGN KEY (`noteid`) REFERENCES `note` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT)
### The error may exist in com/example/memo/mapper/NoteMapper.java (best guess)
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: DELETE FROM note WHERE id=?
### Cause: java.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`memo`.`collection`, CONSTRAINT `fk_collection_note_1` FOREIGN KEY (`noteid`) REFERENCES `note` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT)
; Cannot delete or update a parent row: a foreign key constraint fails (`memo`.`collection`, CONSTRAINT `fk_collection_note_1` FOREIGN KEY (`noteid`) REFERENCES `note` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT); nested exception is java.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`memo`.`collection`, CONSTRAINT `fk_collection_note_1` FOREIGN KEY (`noteid`) REFERENCES `note` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT)] with root cause

 [已解决]删除失败报错:java.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row._第1张图片

[已解决]删除失败报错:java.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row._第2张图片

原因:note表中id为20的记录是collection表的主键,外键约束,不能直接删除主键是别的表的外键的记录

[已解决]删除失败报错:java.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row._第3张图片

[已解决]删除失败报错:java.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row._第4张图片

解决方法:先删除collection表的该记录(主键表),再删除note表的记录 

你可能感兴趣的:(前后端分离,java,开发语言)