3

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?

Ashish Jani
  • 295
  • 3
  • 14
sanghavi7
  • 758
  • 1
  • 15
  • 38

2 Answers2

5

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

Sudhir N
  • 4,008
  • 1
  • 22
  • 32
  • 1
    if I choose option 2 then, on category delete, grails will delete incident and problem records!!!, so it is not good idea... other solution if possible, which handle by grails and thanx...sudhir – sanghavi7 Jun 09 '12 at 09:04
  • If it solves your problem - you can accept the answer. Also - do you want problems/incidents to exist without category or your want to delete them - I am not clear yet. – Sudhir N Jun 09 '12 at 09:36
  • I want both problem and incident to exists without category. – sanghavi7 Jun 09 '12 at 10:17
  • If category is used in any of them object them then its difficult to delete it. – sanghavi7 Jun 09 '12 at 10:18
  • do you got my problem or not? – sanghavi7 Jun 09 '12 at 12:47
  • In this case my first code snippet should work for you, update Incident and problem and set category to null. Note you will need to have category nullable in both incident and problem domains. – Sudhir N Jun 09 '12 at 14:56
  • yeah @sudhir, you are right and I HAVE DONE IT. Is there any other solution? – sanghavi7 Jun 09 '12 at 15:51
0

How your Category looks like, is it belongsTo Incident domain class,if category belongs to some domain class you can not delete it. Ref : See here

Akhilesh
  • 1,400
  • 3
  • 15
  • 20
  • no category is not belongs to that...Its independent class which is referenced by other classes. – sanghavi7 Jun 09 '12 at 06:40
  • if category does not belong to anyone there shouldn't be any problem in deleting, strange.Can you show the domain class of category – Akhilesh Jun 09 '12 at 06:44