Child class object can not delete

grails, grails-controller, grails-domain-class, grails-orm

Solution

The problem is - you have reference to Category in Incident and Problem classes, so database tables for those classes will have Foreign key on category table, so you can not delete a category untill you either remove those incidents/problems or update those incidents problems and set category to null (you will have to make them as nullable in domain constraints)

So either you do

Problem.executeUpdate('update Problem p set category = null where category = ?', [category])

Same for incidents

Or you can model your domain classes using belongsTo and hasMany and grails will handle every thing automatically

Some thing like

class Problem {
    static belongsTo = [category:Category]
}

class Category {
  static hasMany = [
     problems: Problem
 ]
 static mappings = {
   problems cascade: "all-delete-orphan"
 }
}

I would prefer to manage relationships using belongsTo, hasMany, hasOne rather then just using references, it expresses the model better.

It depends on your domain model as well, in your business can problems, incidents exist without a category ! or they must belong to some category. If your answer is first option, your dont want to cascade delete, but update those incidents/problems with null category, if your answer is second option - you needs cascade all-delete-orphan

Problem

I have some domain class Incident,Problem, Category, Impact, Urgency etc. ``` Class Incident { Category category String subject Impact impact } Class Problem { Urgency urgency Category category String title } Class Category { String categoryName String description } ``` now, some rows are inserted into this class. now if I am deleting category it throws error like 'grails cannot delete or update a parent row'.. so what I have to do for deleting?

Original source