删除父行中的grails外键错误
我有以下情况:删除父行中的grails外键错误,grails,foreign-key-relationship,Grails,Foreign Key Relationship,我有以下情况: class Receipt { BigDecimal totalAmount; Date releaseDate; @NotNull Integer vatPercentage; @NotNull Integer discount; Boolean isPayed; Boolean isInvoice; Boolean hasStampDuty; Integer documentNumber; @Not
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域类,如本文所述。
一切正常,但当我尝试删除收据实例时,我看到以下错误:
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\u HealthService实例?我需要更改什么才能允许自动删除并删除错误?类
收据\u HealthService
没有项下的,因此收据的删除不会层叠。看
您可以按以下方式更改收据\u HealthService
:
class Receipt_HealthService {
...
static belongsTo = [receipt: Receipt]
}
或者尝试明确定义级联行为(请参阅)
另一个可能性是在收据
中添加一个beforeDelete
,负责删除healthServices
中的每个条目。看
对于最后一个选项,您可以检查Spring安全插件
中的类User
、UserRole
和Role
,作为示例。我没有在网上找到一个示例项目,但您可以在示例项目中安装插件,然后运行grails s2 quickstart
,查看用户在删除之前的样子(请参见)。谢谢!是否执行以下操作将字段添加到收据表?字段?我不这么认为。从双方访问关系的语义将是相同的(receipt.healthServices和receipt\u HealthService.receipt)。DB方面也应该是相同的:receipt\u health\u服务应该有一个名为receipt\u id的FK。