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.

链接地址: http://www.djcxy.com/p/24328.html

上一篇: 孩子懒惰得到孩子ID

下一篇: Grails GORM和MYSQL级联删除问题