简体   繁体   中英

Grails Many to one Relationship deletion

class First {
    String text
    Second second

    static constraints = {
    }
}

class Second {
    String name

    static constraints = {
    }
}

When I delete Second class object, I got an error like this:

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

I want to delete only the instance of Second inside First.

You have to remove the association of Second from First (aka FK constraint) and then will be able to delete Second .

first.second = null
second.delete()

Refer removeFrom for more details when one-many & many-many relations are used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM