gpt4 book ai didi

grails - 在删除父行中出现grails外键错误

转载 作者:行者123 更新时间:2023-12-02 15:05:45 25 4
gpt4 key购买 nike

我有以下情况:

class Receipt {

BigDecimal totalAmount;
Date releaseDate;

@NotNull
Integer vatPercentage;

@NotNull
Integer discount;

Boolean isPayed;
Boolean isInvoice;
Boolean hasStampDuty;

Integer documentNumber;

@NotNull
static belongsTo = [patient:Patient, doctor:Doctor]

@NotNull
static hasMany = [healthServices:Receipt_HealthService]


static constraints = {
healthServices(blank: false)
patient(blank: false)
totalAmount(blank: false, )
vatPercentage(blank: false, nullable: false)

}


}


class HealthService {

int vat;
String description;
BigDecimal price;

static belongsTo = [healthServiceType:HealthServiceType, doctor:Doctor]

static constraints = {
healthServiceType(blank: false)
vat(size: 11..11)
description(maxSize: 255)

}
}


class Receipt_HealthService {

Receipt receipt
HealthService healthService
int quantity = 1

static constraints = {
}
}

我已经手动创建了Receipt_HealthService域类,如 here中所述。
一切正常,但是当我尝试删除Receipt实例时,出现以下错误:
Cannot delete or update a parent row: a foreign key constraint fails
(`my_localdb`.`receipt_health_service`, CONSTRAINT
`FK96DE98B9D3292D2C` FOREIGN KEY (`receipt_id`) REFERENCES `receipt`(`id`))

为什么会这样?为什么没有自动删除Receipt_HealthService实例?我需要更改什么以允许自动删除和删除错误?

最佳答案

Receipt_HealthService没有belongsTo,因此Receipt的删除没有级联。参见http://grails.github.io/grails-doc/latest/guide/GORM.html#cascades

您可以按以下方式更改Receipt_HealthService:

class Receipt_HealthService {
...
static belongsTo = [receipt: Receipt]
}

或尝试显式定义级联行为(请参见 http://grails.github.io/grails-doc/latest/guide/GORM.html#customCascadeBehaviour)。

另一个可能性是在 beforeDelete中添加一个 Receipt,以删除 healthServices中的每个条目。参见 http://grails.github.io/grails-doc/latest/guide/GORM.html#eventsAutoTimestamping

对于最后一个选项,您可以作为示例查看 User中的 UserRoleRoleSpring Security Plugin类。我没有在线找到示例项目,但是您可以将插件安装在示例项目中,然后运行 grails s2-quickstart来查看 User#beforeDelete的外观(请参阅 https://grails-plugins.github.io/grails-spring-security-core/ref/Scripts/s2-quickstart.html)。

关于grails - 在删除父行中出现grails外键错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31534588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com