如何覆盖 Grails GORM 中关系的级联删除?

发布于 2024-10-01 19:14:25 字数 590 浏览 1 评论 0原文

我在使用 Grails 的 GORM 部分时遇到一些问题。我正在使用 Grails 1.3.4 和 H2。

在数据库中,我有两个表模板报告。在 GORM 级别,我有两个域类 TemplateReport

class Template {

static hasMany = [reports: Report]

...
}

默认

class Report {

static belongsTo = [template: Template]

...
}

行为似乎是,当删除 Template 时,删除操作将被级联,以便它拥有的所有 Report 也将被删除。 在数据库级别,我尝试将 report 表中的 template_id 列设置为 ON DELETE SET NULL 外键,但这并没有不工作。

有没有办法覆盖级联删除?

I'm having some problems with the GORM part of Grails. I am using Grails 1.3.4, together with H2.

In the database I have two tables template and report. On the GORM-level I have the two Domain classes Template and Report;

class Template {

static hasMany = [reports: Report]

...
}

and

class Report {

static belongsTo = [template: Template]

...
}

Default behaviour seems to be that when a Template is deleted, the deletion will be cascaded so that all Reports that it has will be deleted as well.
On the database level I tried to make the template_id-column in the report-table be a ON DELETE SET NULL foreign key , but that didn't work.

Is there some way to override the cascade delete?

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

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

发布评论

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

评论(1

乱世争霸 2024-10-08 19:14:26

应在 Template 类中添加以下内容:

static mapping = {
  reports cascade: 'none'
}

为了能够毫无问题地删除 Template,向 Report 类添加此内容也是必要的:

static constraints = {
  template(nullable: true)
}

The following should be added in the Template class:

static mapping = {
  reports cascade: 'none'
}

to be able to delete Templates without problems, this addition to the Report class is also necessary:

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