【发布时间】:2016-07-11 20:14:27
【问题描述】:
我的Grails 3.0.9 应用程序中有多个域类,它们之间有many-to-many 关系。当我想从数据库中删除所有实体时,我收到一条错误消息:
org.springframework.dao.DataIntegrityViolationException: Hibernate operation: could not execute statement; SQL [n/a]; Cannot delete or update a parent row: a foreign key constraint fails (`database`.`figure`, CONSTRAINT `FK_dbgwxhc7ggggypvmf967wvgfw` FOREIGN KEY (`fig_other_table_id`) REFERENCES `other_table` (`id`)); nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`database`.`figure`, CONSTRAINT `FK_dbgwxhc7ggggypvmf967wvgfw` FOREIGN KEY (`fig_other_table_id`) REFERENCES `other_table` (`id`))
这次有没有办法使用GORM 忽略外键约束?
我试图告诉域类中的 GORM 像这样级联删除:
static mapping = {
figOtherTable cascade: 'all-delete-orphan'
}
就像GORM docs 中描述的那样,但这对我不起作用(或者我遗漏了一些东西)。
我读过here,你可以通过执行来告诉 MySQL 忽略外键约束:
SET FOREIGN_KEY_CHECKS = 0
有没有办法用 GORM 做到这一点?
【问题讨论】:
标签: mysql grails grails-orm grails-3.0