protocbuffer file: XXX_NoUnkeyedLiteral struct{} json:"-"

db.Find(&Srt) raise error:
     field XXX_NoUnkeyedLiteral: define a valid foreign key for relations or implement the Valuer/Scanner interface
HOW:add filter: field.Tag.Get("json") != "-"
WHY:unsupported parsing type: struct{}    set guessLevel to guessEmbeddedHas
     have been commented:schema/relationship.go:381
     as follows:
     reguessOrErr := func() {
     		switch cgl {
     		case guessGuess:
     			schema.guessRelation(relation, field, guessBelongs)
     		case guessBelongs:
     			schema.guessRelation(relation, field, guessEmbeddedBelongs)
     		case guessEmbeddedBelongs:
     			schema.guessRelation(relation, field, guessHas)
     		case guessHas:
     			schema.guessRelation(relation, field, guessEmbeddedHas)
     		// case guessEmbeddedHas:
     		default:
     			schema.err = fmt.Errorf("invalid field found for struct %v's field %s: define a valid foreign key for relations or implement the Valuer/Scanner interface", schema, field.Name)
     		}
     	}
This commit is contained in:
wangjp01 2021-10-11 18:33:12 +08:00
parent 418c60c83c
commit f3bc7427be
2 changed files with 2 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import (
"strings"
"github.com/jinzhu/inflection"
"gorm.io/gorm/clause"
)

View File

@ -134,7 +134,7 @@ func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error)
if fieldStruct := modelType.Field(i); ast.IsExported(fieldStruct.Name) {
if field := schema.ParseField(fieldStruct); field.EmbeddedSchema != nil {
schema.Fields = append(schema.Fields, field.EmbeddedSchema.Fields...)
} else {
} else if field.Tag.Get("json") != "-" {
schema.Fields = append(schema.Fields, field)
}
}