fix: invalid relations detect

This commit is contained in:
2025-03-18 00:04:23 +07:00
parent 9d14fa7c57
commit fefc7a701b
3 changed files with 32 additions and 2 deletions

View File

@@ -98,6 +98,9 @@ func CheckOneToMany(pass *analysis.Pass, models map[string]common.Model) {
if common.IsSlice(field.Type) { if common.IsSlice(field.Type) {
continue continue
} }
if field.Tags.HasParam("many2many") {
continue
}
baseType := common.ResolveBaseType(field.Type) baseType := common.ResolveBaseType(field.Type)
if baseType == nil { if baseType == nil {
@@ -118,6 +121,8 @@ func CheckOneToMany(pass *analysis.Pass, models map[string]common.Model) {
foundBelongsTo := IsBelongsTo(field, model, *relatedModel) foundBelongsTo := IsBelongsTo(field, model, *relatedModel)
if foundBelongsTo { if foundBelongsTo {
fmt.Printf("Found belongs to relation in model `%s` with model `%s`\n", model.Name, *baseType) fmt.Printf("Found belongs to relation in model `%s` with model `%s`\n", model.Name, *baseType)
} else {
pass.Reportf(field.Pos, "Invalid relation in field `%s`", field.Name)
} }
} }
} }

View File

@@ -50,3 +50,14 @@ type File struct {
PostId uint PostId uint
Post Post Post Post
} }
type Consumer struct {
Id uint `gorm:"primaryKey"`
Name string
ShoppingCart ShoppingCart // want "Invalid relation in field `ShoppingCart`"
}
type ShoppingCart struct {
Id uint `gorm:"primaryKey"`
SerializedItems string
}

View File

@@ -26,8 +26,8 @@ type Kuzbass struct {
} }
type City struct { type City struct {
Id uint `gorm:"primaryKey"` Id uint `gorm:"primaryKey"`
Kuzbass Kuzbass Kuzbass Kuzbass // want "Invalid relation in field `Kuzbass`"
} }
type Federation struct { // want "Id field should be presented model \"Federation\"" type Federation struct { // want "Id field should be presented model \"Federation\""
@@ -38,3 +38,17 @@ type Land struct {
Id uint `gorm:"primaryKey"` Id uint `gorm:"primaryKey"`
FederationId uint FederationId uint
} }
// Belongs to
type Owner struct {
Id uint `gorm:"primaryKey"`
Name string
CompanyId int
Company Company
}
type Company struct {
Id int
Name string
}