From 6a0fca21952b1852bece7aa4479099adbb205f56 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 6 Dec 2020 18:07:12 +0800 Subject: [PATCH] Return error for invalid relations definition, close #3830 --- schema/relationship.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/schema/relationship.go b/schema/relationship.go index 9cfc10be..19945e0f 100644 --- a/schema/relationship.go +++ b/schema/relationship.go @@ -362,7 +362,7 @@ func (schema *Schema) guessRelation(relation *Relationship, field *Field, gl gue schema.guessRelation(relation, field, guessEmbeddedHas) // case guessEmbeddedHas: default: - schema.err = fmt.Errorf("invalid field found for struct %v's field %v, need to define a foreign key for relations or it need to implement the Valuer/Scanner interface", schema, field.Name) + schema.err = fmt.Errorf("invalid field found for struct %v's field %v, need to define a valid foreign key for relations or it need to implement the Valuer/Scanner interface", schema, field.Name) } } @@ -427,7 +427,7 @@ func (schema *Schema) guessRelation(relation *Relationship, field *Field, gl gue } } } else if len(primaryFields) == 0 { - if len(foreignFields) == 1 { + if len(foreignFields) == 1 && primarySchema.PrioritizedPrimaryField != nil { primaryFields = append(primaryFields, primarySchema.PrioritizedPrimaryField) } else if len(primarySchema.PrimaryFields) == len(foreignFields) { primaryFields = append(primaryFields, primarySchema.PrimaryFields...)