So I have 2 tables in grails domain class one is Customer and another one is Customer Client.Customer client have a foreign key of Customer. Here is the code for my Customer domain:
class Customer {
String code
String name
String contactPerson
String status
static hasOne = [customerClient: CustomerClient]
static constraints = {
status maxSize:8
contactPerson nullable: true
name nullable: false
status nullabale: false
customerClient nullable: true
}
}
and here is the code for my Customer Client domain:
package workdatabase4
class CustomerClient {
String zendeskApiToken
String zendeskUrl
String clientChannelId
String clientName
String clientChannel
String status
String zendeskApiAuth
String channelAccessToken
static belongsTo = [customer: Customer]
static constraints = {
clientName nullable: true
}
}
I do not have a delete function in my controller,I just tried to delete it manually through the database in intellij and I got this error:
[23000][1451] Cannot delete or update a parent row: a foreign key constraint fails (`customerclient`.`customer_client`, CONSTRAINT `FKgobbh6gqke89v6a7rdsfcdu2o` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`id`))
My question is how can I delete Customer without deleting the Customer Client first? Thank you.
This is called Cascade Delete. The point is if you delete the parent row, this will leave a bunch of child rows in the database that you cannot locate anymore.
I am new to Grails too. I am not sure if this will work or not.
I google "grails cascade". Google points me to the grails doc page. It has an example that says to add the cascade entry to the mapping block to the domain class.
If this does not work, you will have to change the cascade delete setting from the database system directly. The database may block you from deleting the parent row.
class Author {
static hasMany = [books: Book]
static mapping = {
books cascade: 'all-delete-orphan'
}
}