Grails:无法删除或更新父行:外键约束失败
所以我在grails域类中有两个表,一个是Customer,另一个是Customer Client。以下是我的客户域的代码:Grails:无法删除或更新父行:外键约束失败,grails,groovy,gorm,Grails,Groovy,Gorm,所以我在grails域类中有两个表,一个是Customer,另一个是Customer Client。以下是我的客户域的代码: class Customer { String code String name String contactPerson String status static hasOne = [customerClient: CustomerClient] static constraints = { sta
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
}
}
以下是我的客户域的代码:
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
}
}
我的控制器中没有删除功能,我只是试图通过intellij中的数据库手动删除它,但出现以下错误:
[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`))
我的问题是如何在不先删除客户的情况下删除客户?
谢谢。这称为级联删除。关键是,如果删除父行,将在数据库中留下一堆无法再定位的子行 我对Grails也是新手。我不确定这是否有效 我在谷歌上搜索“GrailsCascade”。谷歌将我指向grails文档页面。它有一个示例,说明将级联条目添加到映射块到域类中 如果这不起作用,则必须直接从数据库系统更改级联删除设置。数据库可能会阻止您删除父行
class Author {
static hasMany = [books: Book]
static mapping = {
books cascade: 'all-delete-orphan'
}
}