diff --git a/relationsCheck/relationsCheck.go b/relationsCheck/relationsCheck.go index 9cb21fc..9f4d374 100644 --- a/relationsCheck/relationsCheck.go +++ b/relationsCheck/relationsCheck.go @@ -98,6 +98,9 @@ func CheckOneToMany(pass *analysis.Pass, models map[string]common.Model) { if common.IsSlice(field.Type) { continue } + if field.Tags.HasParam("many2many") { + continue + } baseType := common.ResolveBaseType(field.Type) if baseType == nil { @@ -118,6 +121,8 @@ func CheckOneToMany(pass *analysis.Pass, models map[string]common.Model) { foundBelongsTo := IsBelongsTo(field, model, *relatedModel) if foundBelongsTo { 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) } } } diff --git a/tests/testdata/src/relations_check/negative.go b/tests/testdata/src/relations_check/negative.go index b14d80d..d532061 100644 --- a/tests/testdata/src/relations_check/negative.go +++ b/tests/testdata/src/relations_check/negative.go @@ -50,3 +50,14 @@ type File struct { PostId uint 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 +} diff --git a/tests/testdata/src/relations_check/positive.go b/tests/testdata/src/relations_check/positive.go index 67f4e27..53ae288 100644 --- a/tests/testdata/src/relations_check/positive.go +++ b/tests/testdata/src/relations_check/positive.go @@ -26,8 +26,8 @@ type Kuzbass struct { } type City struct { - Id uint `gorm:"primaryKey"` - Kuzbass Kuzbass + Id uint `gorm:"primaryKey"` + Kuzbass Kuzbass // want "Invalid relation in field `Kuzbass`" } type Federation struct { // want "Id field should be presented model \"Federation\"" @@ -38,3 +38,17 @@ type Land struct { Id uint `gorm:"primaryKey"` FederationId uint } + +// Belongs to + +type Owner struct { + Id uint `gorm:"primaryKey"` + Name string + CompanyId int + Company Company +} + +type Company struct { + Id int + Name string +}