From 53f8c9fc1c5d24324308673cc9ae3afd4442516a Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 30 Aug 2020 20:57:58 +0800 Subject: [PATCH] More compatible prioritized primary field #3156 --- schema/schema.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/schema/schema.go b/schema/schema.go index 458256d1..ea81d683 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -161,13 +161,18 @@ func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error) field.setupValuerAndSetter() } - if f := schema.LookUpField("id"); f != nil { - if f.PrimaryKey { - schema.PrioritizedPrimaryField = f + prioritizedPrimaryField := schema.LookUpField("id") + if prioritizedPrimaryField == nil { + prioritizedPrimaryField = schema.LookUpField("ID") + } + + if prioritizedPrimaryField != nil { + if prioritizedPrimaryField.PrimaryKey { + schema.PrioritizedPrimaryField = prioritizedPrimaryField } else if len(schema.PrimaryFields) == 0 { - f.PrimaryKey = true - schema.PrioritizedPrimaryField = f - schema.PrimaryFields = append(schema.PrimaryFields, f) + prioritizedPrimaryField.PrimaryKey = true + schema.PrioritizedPrimaryField = prioritizedPrimaryField + schema.PrimaryFields = append(schema.PrimaryFields, prioritizedPrimaryField) } }