From 715d7951aa7a19d38aea6fb66e2bb8f381bafbf9 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 25 Dec 2015 17:36:47 +0800 Subject: [PATCH] Test HasOne relations for normal foreign key or scanner --- association_test.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/association_test.go b/association_test.go index e538bbe4..6eb9f773 100644 --- a/association_test.go +++ b/association_test.go @@ -10,9 +10,10 @@ func TestBelongsTo(t *testing.T) { DB.CreateTable(Category{}, Post{}) post := Post{ - Title: "post 1", - Body: "body 1", - Category: Category{Name: "Category 1"}, + Title: "post 1", + Body: "body 1", + Category: Category{Name: "Category 1"}, + MainCategory: Category{Name: "Main Category 1"}, } if err := DB.Save(&post).Error; err != nil { @@ -26,6 +27,12 @@ func TestBelongsTo(t *testing.T) { t.Errorf("Query has one relations with Association") } + var mainCategory Category + DB.Model(&post).Association("MainCategory").Find(&mainCategory) + if mainCategory.Name != "Main Category 1" { + t.Errorf("Query has one relations with Association") + } + var category1 Category DB.Model(&post).Related(&category1) if category1.Name != "Category 1" {