GORM在删除时的多对一关系中设置外键

时间:2013-01-05 19:27:06

标签: grails constraints gorm

我的域名:

Company {
   String name
}

Contact {
   String name
   Company compa

   static constraints = {
      compa (nullable: true)
   }
}

如果公司有来自联系人的外键约束,则无法删除公司。我希望删除工作,并在删除公司时将compa属性设置为null。

是否存在这样的约束?有没有比我尝试更好的方法呢?

1 个答案:

答案 0 :(得分:3)

尝试一下,也许还有其他选择。我没有测试过这段代码,只是为了给你一个想法。

在Company.groovy中:

 def beforeDelete() {
      Contact.withNewSession {
          Contact.findAllByCompany(this).each {
            it.company = null
            it.save()
          }
      }
 }
相关问题