删除父行中的grails外键错误
grails foreign key error in delete parent row
我有以下情况:
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 域 class,如 post 中所述。
一切正常,但是当我尝试删除 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 个实例没有自动删除?我需要更改什么才能允许自动删除并消除错误?
class 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
对于最后一个选项,您可以检查 Spring Security Plugin
中的 classes User
、UserRole
和 Role
作为示例。我没有在网上找到示例项目,但您可以在示例项目中安装插件,然后 运行 grails s2-quickstart
看看 User#beforeDelete
的样子(参见 https://grails-plugins.github.io/grails-spring-security-core/ref/Scripts/s2-quickstart.html ).
我有以下情况:
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 域 class,如 post
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 个实例没有自动删除?我需要更改什么才能允许自动删除并消除错误?
class 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
对于最后一个选项,您可以检查 Spring Security Plugin
中的 classes User
、UserRole
和 Role
作为示例。我没有在网上找到示例项目,但您可以在示例项目中安装插件,然后 运行 grails s2-quickstart
看看 User#beforeDelete
的样子(参见 https://grails-plugins.github.io/grails-spring-security-core/ref/Scripts/s2-quickstart.html ).