feat: delete constraint for belongs to and has one

This commit is contained in:
2025-03-18 03:29:00 +07:00
parent 4536be4d10
commit ffbeccc4fd
4 changed files with 10 additions and 4 deletions

View File

@@ -8,7 +8,7 @@ import (
func CheckCascadeDelete(pass *analysis.Pass, field common.Field) bool {
if !field.Tags.HasParam("constraint") {
pass.Reportf(field.Pos, "field %s should have a constraint", field.Name)
pass.Reportf(field.Pos, "field %s should have a delete constraint", field.Name)
return true
}
constraintValue := field.Tags.GetParam("constraint").Value

View File

@@ -123,8 +123,14 @@ func CheckOneToMany(pass *analysis.Pass, models map[string]common.Model) {
if !foundOneToMany {
if foundBelongsTo {
fmt.Printf("`%s` belongs `%s`\n", *baseType, model.Name)
if CheckCascadeDelete(pass, field) {
return
}
} else if hasOne {
fmt.Printf("`%s` has one `%s` \n", model.Name, relatedModel.Name)
if CheckCascadeDelete(pass, field) {
return
}
} else {
pass.Reportf(field.Pos, "Invalid relation in field `%s`", field.Name)
}

View File

@@ -66,7 +66,7 @@ type ShoppingCart struct {
type Hotel struct {
Id uint `gorm:"primaryKey"`
Office
Office // want "field Office should have a delete constraint"
}
type Office struct {

View File

@@ -45,7 +45,7 @@ type Owner struct {
Id uint `gorm:"primaryKey"`
Name string
CompanyId int
Company Company
Company Company `gorm:"constraint:OnDelete:CASCADE;"`
}
type Company struct {