【问题标题】:Delete all entities from database with gorm while ignoring foreign key constraint使用 gorm 从数据库中删除所有实体,同时忽略外键约束
【发布时间】: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


    【解决方案1】:

    我相信 GORM 不能在多对多关系中级联删除。您必须自己处理删除...

    此链接也可能对您有所帮助http://spring.io/blog/2010/07/02/gorm-gotchas-part-2/

    【讨论】:

      猜你喜欢
      • 2012-04-12
      • 2020-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      • 2021-04-02
      • 1970-01-01
      相关资源
      最近更新 更多