如何覆盖Grails GORM中关系的级联删除? [英] How do I override the cascade delete for a relation in Grails GORM?
问题描述
Template
和 Report
; class Template {
static hasMany = [reports:Report]
...
}
和
static belongsTo = [template:Template]
...
}
默认行为似乎是在删除 Template
时删除将被级联,所有报告
s也将被删除。
在数据库级别上,我尝试使报告中的 template_id 列 -table成为 ON DELETE SET NULL 外键,但没有奏效。
是否有一些方法可以覆盖级联删除?
报告级联:'none'
}
为了能够毫无问题的删除 Template
,除了 Report
类也是必须的: / p>
static constraints = {
template(nullable:true)
}
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 Report
s 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?
The following should be added in the Template
class:
static mapping = {
reports cascade: 'none'
}
to be able to delete Template
s without problems, this addition to the Report
class is also necessary:
static constraints = {
template(nullable: true)
}
这篇关于如何覆盖Grails GORM中关系的级联删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!