feat: delete constraint for belongs to and has one
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user