Grails GORM和MYSQL级联删除问题

发布于 2024-10-06 20:48:03 字数 469 浏览 3 评论 0 原文

我有一个表 User,它在静态 hasMany 下的 User 类中定义了许多子表 圣杯。

我在 grails 中执行 User.get(3).delete() 时没有问题。它会自动删除该用户及其所有子表行。 但是当我想在 MySQL Workbench 中执行相同的操作时。我收到 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'

我不知道 MySQL 出了什么问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在你怀里撒娇 2024-10-13 20:48:03

您在哪里读到文档中的“ON DELETE CASCADE”?我还没有找到它 这篇文章的发布者必须手动添加此内容以及获得所需的行为。

如果 grails 确实应该添加这个,您是否尝试过删除数据库并由 grails 重新创建它(至少在开发环境中)?也许当前的模式是在添加belongsTo 或其他内容之前生成的?

另请查看 这篇关于 GORM 问题的博文,涉及 hasMany 和 ownTo .

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文