0

I have multiple domain classes in my Grails 3.0.9 application which have many-to-many relations to each other. When I want to delete all entities from the database I get an error saying:

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`))

Is there a way using GORM to ignore foreign key contraints this time?

I've tried to tell GORM in the domain class to cascade delete like this:

static mapping = {
        figOtherTable cascade: 'all-delete-orphan'
}

like descriped in the GORM docs, but that's not working for me (or I am missing something).

I've read here that you can tell MySQL to ignore foreign key constraints by executing:

SET FOREIGN_KEY_CHECKS = 0 

Is there a way doing this with GORM?

Community
  • 1
  • 1
Peter
  • 1,679
  • 2
  • 31
  • 60

1 Answers1

0

I believe that GORM is not able to cascade deletes in a many-to-many relationship. You have to handle deletes on your own...

This Link might help you as well http://spring.io/blog/2010/07/02/gorm-gotchas-part-2/

feuernurmitm
  • 312
  • 2
  • 11