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 {
|
func CheckCascadeDelete(pass *analysis.Pass, field common.Field) bool {
|
||||||
if !field.Tags.HasParam("constraint") {
|
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
|
return true
|
||||||
}
|
}
|
||||||
constraintValue := field.Tags.GetParam("constraint").Value
|
constraintValue := field.Tags.GetParam("constraint").Value
|
||||||
|
|||||||
@@ -123,8 +123,14 @@ func CheckOneToMany(pass *analysis.Pass, models map[string]common.Model) {
|
|||||||
if !foundOneToMany {
|
if !foundOneToMany {
|
||||||
if foundBelongsTo {
|
if foundBelongsTo {
|
||||||
fmt.Printf("`%s` belongs `%s`\n", *baseType, model.Name)
|
fmt.Printf("`%s` belongs `%s`\n", *baseType, model.Name)
|
||||||
|
if CheckCascadeDelete(pass, field) {
|
||||||
|
return
|
||||||
|
}
|
||||||
} else if hasOne {
|
} else if hasOne {
|
||||||
fmt.Printf("`%s` has one `%s` \n", model.Name, relatedModel.Name)
|
fmt.Printf("`%s` has one `%s` \n", model.Name, relatedModel.Name)
|
||||||
|
if CheckCascadeDelete(pass, field) {
|
||||||
|
return
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
pass.Reportf(field.Pos, "Invalid relation in field `%s`", field.Name)
|
pass.Reportf(field.Pos, "Invalid relation in field `%s`", field.Name)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,8 +65,8 @@ type ShoppingCart struct {
|
|||||||
// Has one
|
// Has one
|
||||||
|
|
||||||
type Hotel struct {
|
type Hotel struct {
|
||||||
Id uint `gorm:"primaryKey"`
|
Id uint `gorm:"primaryKey"`
|
||||||
Office
|
Office // want "field Office should have a delete constraint"
|
||||||
}
|
}
|
||||||
|
|
||||||
type Office struct {
|
type Office struct {
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ type Owner struct {
|
|||||||
Id uint `gorm:"primaryKey"`
|
Id uint `gorm:"primaryKey"`
|
||||||
Name string
|
Name string
|
||||||
CompanyId int
|
CompanyId int
|
||||||
Company Company
|
Company Company `gorm:"constraint:OnDelete:CASCADE;"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Company struct {
|
type Company struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user