forked from mirror/gorm
fix: primaryFields are overwritten (#5721)
This commit is contained in:
parent
9564b82975
commit
4b22a55a75
|
@ -403,33 +403,30 @@ func (schema *Schema) guessRelation(relation *Relationship, field *Field, cgl gu
|
||||||
case guessBelongs:
|
case guessBelongs:
|
||||||
primarySchema, foreignSchema = relation.FieldSchema, schema
|
primarySchema, foreignSchema = relation.FieldSchema, schema
|
||||||
case guessEmbeddedBelongs:
|
case guessEmbeddedBelongs:
|
||||||
if field.OwnerSchema != nil {
|
if field.OwnerSchema == nil {
|
||||||
primarySchema, foreignSchema = relation.FieldSchema, field.OwnerSchema
|
|
||||||
} else {
|
|
||||||
reguessOrErr()
|
reguessOrErr()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
primarySchema, foreignSchema = relation.FieldSchema, field.OwnerSchema
|
||||||
case guessHas:
|
case guessHas:
|
||||||
case guessEmbeddedHas:
|
case guessEmbeddedHas:
|
||||||
if field.OwnerSchema != nil {
|
if field.OwnerSchema == nil {
|
||||||
primarySchema, foreignSchema = field.OwnerSchema, relation.FieldSchema
|
|
||||||
} else {
|
|
||||||
reguessOrErr()
|
reguessOrErr()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
primarySchema, foreignSchema = field.OwnerSchema, relation.FieldSchema
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(relation.foreignKeys) > 0 {
|
if len(relation.foreignKeys) > 0 {
|
||||||
for _, foreignKey := range relation.foreignKeys {
|
for _, foreignKey := range relation.foreignKeys {
|
||||||
if f := foreignSchema.LookUpField(foreignKey); f != nil {
|
f := foreignSchema.LookUpField(foreignKey)
|
||||||
foreignFields = append(foreignFields, f)
|
if f == nil {
|
||||||
} else {
|
|
||||||
reguessOrErr()
|
reguessOrErr()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
foreignFields = append(foreignFields, f)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var primaryFields []*Field
|
|
||||||
var primarySchemaName = primarySchema.Name
|
var primarySchemaName = primarySchema.Name
|
||||||
if primarySchemaName == "" {
|
if primarySchemaName == "" {
|
||||||
primarySchemaName = relation.FieldSchema.Name
|
primarySchemaName = relation.FieldSchema.Name
|
||||||
|
@ -466,10 +463,11 @@ func (schema *Schema) guessRelation(relation *Relationship, field *Field, cgl gu
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(foreignFields) == 0 {
|
switch {
|
||||||
|
case len(foreignFields) == 0:
|
||||||
reguessOrErr()
|
reguessOrErr()
|
||||||
return
|
return
|
||||||
} else if len(relation.primaryKeys) > 0 {
|
case len(relation.primaryKeys) > 0:
|
||||||
for idx, primaryKey := range relation.primaryKeys {
|
for idx, primaryKey := range relation.primaryKeys {
|
||||||
if f := primarySchema.LookUpField(primaryKey); f != nil {
|
if f := primarySchema.LookUpField(primaryKey); f != nil {
|
||||||
if len(primaryFields) < idx+1 {
|
if len(primaryFields) < idx+1 {
|
||||||
|
@ -483,7 +481,7 @@ func (schema *Schema) guessRelation(relation *Relationship, field *Field, cgl gu
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if len(primaryFields) == 0 {
|
case len(primaryFields) == 0:
|
||||||
if len(foreignFields) == 1 && primarySchema.PrioritizedPrimaryField != nil {
|
if len(foreignFields) == 1 && primarySchema.PrioritizedPrimaryField != nil {
|
||||||
primaryFields = append(primaryFields, primarySchema.PrioritizedPrimaryField)
|
primaryFields = append(primaryFields, primarySchema.PrioritizedPrimaryField)
|
||||||
} else if len(primarySchema.PrimaryFields) == len(foreignFields) {
|
} else if len(primarySchema.PrimaryFields) == len(foreignFields) {
|
||||||
|
|
Loading…
Reference in New Issue