简体   繁体   中英

Grails GORM and MYSQL cascade delete problem

I have a table User that has many child tables defined in User class under the static hasMany grails.

I have no problem when doing User.get(3).delete() in grails. It automatically delete that user and all its child table rows. But when I want to perform the same operation in MySQL workbench. I get Error thrown by MySQL:

ERROR 1451: Cannot delete or update a parent row: a foreign key constraint fails (`test_db`.`search_stat`, CONSTRAINT `FK7A3CFFB6E64DB41` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`))
SQL Statement:
DELETE FROM `test_db`.`user` WHERE `id`='3'

I dont know what is the problem here with MySQL.

Where did you read about the "ON DELETE CASCADE" in the documentation? I haven't found it here . The poster of this post had to manually add this as well to get the desired behaviour.

If grails is really supposed to add this, have you tried deleting the DB and having it re-created by grails (at least in development environment)? Maybe the current schema was generated before you added the belongsTo or something?

Also check out this Blog post about GORM Gotchas regarding hasMany and belongsTo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM