diff --git a/relationsCheck/constraints.go b/relationsCheck/constraints.go index 5af74e6..dcfc023 100644 --- a/relationsCheck/constraints.go +++ b/relationsCheck/constraints.go @@ -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 diff --git a/relationsCheck/relationsCheck.go b/relationsCheck/relationsCheck.go index 5360fb3..8a2ab8d 100644 --- a/relationsCheck/relationsCheck.go +++ b/relationsCheck/relationsCheck.go @@ -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) } diff --git a/tests/testdata/src/relations_check/negative.go b/tests/testdata/src/relations_check/negative.go index 47852a0..a6d30fa 100644 --- a/tests/testdata/src/relations_check/negative.go +++ b/tests/testdata/src/relations_check/negative.go @@ -65,8 +65,8 @@ type ShoppingCart struct { // Has one type Hotel struct { - Id uint `gorm:"primaryKey"` - Office + Id uint `gorm:"primaryKey"` + Office // want "field Office should have a delete constraint" } type Office struct { diff --git a/tests/testdata/src/relations_check/positive.go b/tests/testdata/src/relations_check/positive.go index 53ae288..2614199 100644 --- a/tests/testdata/src/relations_check/positive.go +++ b/tests/testdata/src/relations_check/positive.go @@ -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 {