gpt4 book ai didi

mysql - 使用 gorm 删除数据库中的所有实体,同时忽略外键约束

转载 作者:行者123 更新时间:2023-11-29 21:13:08 25 4
gpt4 key购买 nike

我的 Grails 3.0.9 应用程序中有多个域类,它们之间具有多对多关系。当我想从数据库中删除所有实体时,我收到一条错误消息:

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 做到这一点?

最佳答案

我相信 GORM 无法在多对多关系中级联删除。您必须自行处理删除...

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

关于mysql - 使用 gorm 删除数据库中的所有实体,同时忽略外键约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36196796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com