Test HasOne relations for normal foreign key or scanner

This commit is contained in:
Jinzhu 2015-12-25 17:36:47 +08:00
parent affb64b04b
commit 715d7951aa
1 changed files with 10 additions and 3 deletions

View File

@ -10,9 +10,10 @@ func TestBelongsTo(t *testing.T) {
DB.CreateTable(Category{}, Post{}) DB.CreateTable(Category{}, Post{})
post := Post{ post := Post{
Title: "post 1", Title: "post 1",
Body: "body 1", Body: "body 1",
Category: Category{Name: "Category 1"}, Category: Category{Name: "Category 1"},
MainCategory: Category{Name: "Main Category 1"},
} }
if err := DB.Save(&post).Error; err != nil { 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") 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 var category1 Category
DB.Model(&post).Related(&category1) DB.Model(&post).Related(&category1)
if category1.Name != "Category 1" { if category1.Name != "Category 1" {