Search code examples
databasegrailsrelationcascading-deletes

grails, relations and cascade delete


I have the following domain classes:

class Patient {
    ...
}

class Receipt{
@NotNull
static belongsTo = [patient:Patient]
...
}

If I try to delete a Patient instance (after creation of Receipt instances), I have a MySQLIntegrityConstraintViolationException. Notice that a patient can have zero-to-many receipts.


Solution

  • To complete the parent child relationship, create a has many section in the parent domain class:

    class Patient {
    static hasMany = [receipts: Receipt]
        ...
    }
    
    class Receipt{
    @NotNull
    static belongsTo = [patient:Patient]
    ...
    }