Lars Andren
Lars Andren

Reputation: 8771

How do I override the cascade delete for a relation in Grails GORM?

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?

Upvotes: 4

Views: 3616

Answers (1)

Lars Andren
Lars Andren

Reputation: 8771

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)
}

Upvotes: 7

Related Questions