2013-05-15 68 views
2
class First { 
    String text 
    Second second 

    static constraints = { 
    } 
} 

class Second { 
    String name 

    static constraints = { 
    } 
} 

当我删除第二类对象,我得到了这样的错误:Grails的多对一的关系缺失

Cannot delete or update a parent row: a foreign key constraint fails. 

我想删除里面第一页第二页的唯一实例。

+0

http://grails.org/doc/latest/guide/GORM.html#cascades –

+0

我希望第一个对象必须活着,但是当第二个对象首先被删除 – Visme

回答

0

你必须removeSecondFirst(又名FK约束)的协会,然后将能够删除Second

first.second = null 
second.delete() 

参考removeFrom更多细节时,使用了一种一对多&多对多的关系。

+0

时,第二个的域必须为空或为空。 second = null – Visme

+0

从来没有尝试过,使用API​​免除了我的问题。 – dmahapatro

+0

first.second = null second.delete(flush:true)。我们不能使用removeFrom。因为第二个里面第一类是单个对象。不是集合。 – Visme