From f73f7b251f3d44a6e974c2ba0301060d7ab32502 Mon Sep 17 00:00:00 2001 From: Richard Knop Date: Sat, 13 Feb 2016 20:28:42 +0800 Subject: [PATCH] HasTable now works with table name passed as a string. Before, only HasTable(&Foo) would work but HasTable("foos") would always return false. This PR fixes that. --- main_test.go | 12 ++++++++++++ scope.go | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/main_test.go b/main_test.go index 65467d73..8722c7c0 100644 --- a/main_test.go +++ b/main_test.go @@ -165,12 +165,24 @@ func TestHasTable(t *testing.T) { Stuff string } DB.DropTable(&Foo{}) + + // Table should not exist at this point, HasTable should return false + if ok := DB.HasTable("foos"); ok { + t.Errorf("Table should not exist, but does") + } if ok := DB.HasTable(&Foo{}); ok { t.Errorf("Table should not exist, but does") } + + // We create the table if err := DB.CreateTable(&Foo{}).Error; err != nil { t.Errorf("Table should be created") } + + // And now it should exits, and HasTable should return true + if ok := DB.HasTable("foos"); !ok { + t.Errorf("Table should exist, but HasTable informs it does not") + } if ok := DB.HasTable(&Foo{}); !ok { t.Errorf("Table should exist, but HasTable informs it does not") } diff --git a/scope.go b/scope.go index a11d4ec4..2fa4acdb 100644 --- a/scope.go +++ b/scope.go @@ -267,6 +267,10 @@ type dbTabler interface { // TableName get table name func (scope *Scope) TableName() string { + if strTableName, ok := scope.Value.(string); ok { + return strTableName + } + if scope.Search != nil && len(scope.Search.tableName) > 0 { return scope.Search.tableName }