From 28d1eb474b2747cf8935c18b0b2b0939a52a027f Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Mon, 4 Jan 2016 08:45:43 +0800 Subject: [PATCH] Add test case for one to one relation with customized foreign keys --- customize_column_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/customize_column_test.go b/customize_column_test.go index 0a212525..5897b2cd 100644 --- a/customize_column_test.go +++ b/customize_column_test.go @@ -3,6 +3,8 @@ package gorm_test import ( "testing" "time" + + "github.com/jinzhu/gorm" ) type CustomizeColumn struct { @@ -102,3 +104,38 @@ func TestManyToManyWithCustomizedColumn(t *testing.T) { t.Errorf("should preload correct accounts") } } + +type CustomizeUser struct { + gorm.Model + Email string `sql:"column:email_address"` +} + +type CustomizeInvitation struct { + gorm.Model + Address string `sql:"column:invitation"` + Person *CustomizeUser `gorm:"foreignkey:Email;associationforeignkey:invitation"` +} + +func TestOneToOneWithCustomizedColumn(t *testing.T) { + DB.DropTable(&CustomizeUser{}, &CustomizeInvitation{}) + DB.AutoMigrate(&CustomizeUser{}, &CustomizeInvitation{}) + + user := CustomizeUser{ + Email: "hello@example.com", + } + invitation := CustomizeInvitation{ + Address: "hello@example.com", + } + + DB.Create(&user) + DB.Create(&invitation) + + var invitation2 CustomizeInvitation + if err := DB.Preload("Person").Find(&invitation2, invitation.ID).Error; err != nil { + t.Errorf("no error should happen, but got %v", err) + } + + if invitation2.Person.Email != user.Email { + t.Errorf("Should preload one to one relation with customize foreign keys") + } +}