From 3d642c1987b2ee6765014388c0fd0dac8abbc372 Mon Sep 17 00:00:00 2001 From: Paolo Galeone Date: Wed, 2 Apr 2014 11:00:17 +0200 Subject: [PATCH] Add primaryKey field to scope and uses getPrimaryKey to find the one marked in that way, if present. Otherwise fallback to id --- scope.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/scope.go b/scope.go index 40697f9a..9b5df96a 100644 --- a/scope.go +++ b/scope.go @@ -20,6 +20,7 @@ type Scope struct { db *DB _values map[string]interface{} skipLeft bool + primaryKey string } // NewScope create scope for callbacks, including DB's search information @@ -78,7 +79,12 @@ func (scope *Scope) HasError() bool { // PrimaryKey get the primary key's column name func (scope *Scope) PrimaryKey() string { - return "id" + if scope.primaryKey != "" { + return scope.primaryKey + } + + scope.primaryKey = scope.getPrimaryKey() + return scope.primaryKey } // PrimaryKeyZero check the primary key is blank or not @@ -238,7 +244,13 @@ func (scope *Scope) Fields() []*Field { value := indirectValue.FieldByName(fieldStruct.Name) field.Value = value.Interface() field.IsBlank = isBlank(value) - field.isPrimaryKey = scope.PrimaryKey() == field.DBName + + // Search for primary key tag identifier + field.isPrimaryKey = scope.PrimaryKey() == field.DBName || fieldStruct.Tag.Get("primaryKey") != "" + fmt.Printf("Name: %s\n isPrimaryKey: %v\n\n",field.DBName, field.isPrimaryKey) + if field.isPrimaryKey { + scope.primaryKey = field.DBName + } if scope.db != nil { field.Tag = fieldStruct.Tag