
Untitled
By: a guest on
May 23rd, 2012 | syntax:
None | size: 0.89 KB | hits: 8 | expires: Never
Removing child-parent association in GORM
class Business{
static hasMany = [contacts:ContactPerson]
}
class ContactPerson{
}
bob.delete(flush:true)
ERROR: update or delete on table "contact_person" violates foreign key constraint
"fk4a69c6b329ef2fe1" on table "business_contact_person"
Detail: Key (id)=(174) is still referenced from table "business_contact_person".
class Business{
static hasMany = [ contacts:ContactPerson ]
}
class ContactPerson{
static belongsTo = [ business: Business ]
}
def bob = ContactPerson.get(params.id)
def criteria = Business.createCriteria()
def businesses = criteria.listDistinct{
createAlias("contactPersons","c")
eq("c.id", bob.id)
}
businesses.each{business->
business.removeFromContactPersons(bob)
business.save(flush:true)
}
bob.delete(flush:true)
static mapping = {
children cascade:"all-delete-orphan"
}