Grails:无法删除或更新父行:外键约束失败

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

所以我在 grails 域中有 2 个表 class 一个是客户,另一个是客户 Client.Customer 客户有一个外键客户。这是我的客户域的代码:


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`))

我的问题是如何在不先删除 Customer Client 的情况下删除 Customer? 谢谢。

这称为级联删除。关键是如果你删除父行,这将在数据库中留下一堆你无法再定位的子行。

我也是 Grails 新手。我不确定这是否有效。

我google“grails 级联”。 Google 将我指向 grails 文档页面。它有一个例子说将 cascade 条目添加到映射块到域 class.

如果这不起作用,您将不得不直接从数据库系统更改级联删除设置。数据库可能会阻止您删除父行。

class Author {

    static hasMany = [books: Book]

    static mapping = {
        books cascade: 'all-delete-orphan'
    }
}