How to override cascading deletion for a relationship in Grails GORM?

I have problems with the GORM Grails part. I am using Grails 1.3.4 along with H2.

In the database, I have two tables, a template and a report . At the GORM level, I have two domain classes Templateand Report;

class Template {

static hasMany = [reports: Report]

...
}

and

class Report {

static belongsTo = [template: Template]

...
}

The default behavior seems to be that when you delete, the Templatedeletion will be cascaded so that all Reportthat it has will also be deleted. At the database level, I tried to make template_id -column in the -table report the foreign key ON DELETE SET NULL, but this did not work.

Is there a way to override cascade deletion?

+3
1

Template :

static mapping = {
  reports cascade: 'none'
}

Template , Report :

static constraints = {
  template(nullable: true)
}
+6

Source: https://habr.com/ru/post/1773775/


All Articles