From 07af30c54804d0f9617992920f47a5562797b9f7 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 25 Feb 2015 10:56:05 +0800 Subject: [PATCH] Fix primary key in embedded struct --- embedded_struct_test.go | 4 ++++ model_struct.go | 3 +++ 2 files changed, 7 insertions(+) diff --git a/embedded_struct_test.go b/embedded_struct_test.go index de1781dd..17405b74 100644 --- a/embedded_struct_test.go +++ b/embedded_struct_test.go @@ -35,4 +35,8 @@ func TestSaveAndQueryEmbeddedStruct(t *testing.T) { } else if egNews.BasePost.Title != "engadget_news" { t.Errorf("embedded struct's value should be scanned correctly") } + + if DB.NewScope(&HNPost{}).PrimaryKeyField() == nil { + t.Errorf("primary key with embedded struct should works") + } } diff --git a/model_struct.go b/model_struct.go index 0df487fb..d64b93c7 100644 --- a/model_struct.go +++ b/model_struct.go @@ -233,6 +233,9 @@ func (scope *Scope) GetModelStruct(noRelationship ...bool) *ModelStruct { f = f.clone() f.Names = append([]string{fieldStruct.Name}, f.Names...) modelStruct.StructFields = append(modelStruct.StructFields, f) + if f.IsPrimaryKey { + modelStruct.PrimaryKeyField = f + } } break } else {