考虑以下域类
class Business{
static hasMany = [contacts:ContactPerson]
}
class ContactPerson{
}
给定以下域类,假设我们使用以下示例:
Alice is ACME's contact person. Bob and Carol are Calamity Corp's contact person.
假设我想删除 ContactPerson 表中的 Bob。因此:
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".
在此上下文中,会引发异常,因为 Bob 仍与 Calamity Corp 关联。
如何从 ContactPerson 表中删除 Bob? 我想从数据库中完全删除 Bob。我不想添加属于,因为我不想将删除级联到 Business 的子级(也就是说,如果我从数据库中删除 Acme Corp,我仍然希望 Alice 位于系统中)。
我在 disassociating the parent from the child 上看过示例但反之则不然。
最佳答案
我相信只需添加一个 belongsTo
,级联就应该按预期工作......
class Business{
static hasMany = [ contacts:ContactPerson ]
}
class ContactPerson{
static belongsTo = [ business: Business ]
}
关于grails - 删除 GORM 中的子 parent 关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8077064/