Merge 26ed31e7b60ebd86fc00bd2e3e7e2228a4c338db into fd9e42655f382e76a4c755a676be827947f23b81

This commit is contained in:
Jay Taylor 2015-08-07 22:04:08 +00:00
commit 903d18a03e
2 changed files with 65 additions and 1 deletions

View File

@ -130,7 +130,10 @@ func (s *DB) LogMode(enable bool) *DB {
} }
func (s *DB) SingularTable(enable bool) { func (s *DB) SingularTable(enable bool) {
modelStructs = map[reflect.Type]*ModelStruct{} // Don't clobber/reset modelStructs if it is already initialized.
if modelStructs == nil {
modelStructs = map[reflect.Type]*ModelStruct{}
}
s.parent.singularTable = enable s.parent.singularTable = enable
} }

View File

@ -0,0 +1,61 @@
package gorm_test
import (
"testing"
)
type (
Organization struct {
Id int64
Name string `sql:"type:varchar(255);not null;"`
}
App struct {
Id int64
Organization Organization
OrganizationId int64 `sql:"not null;"`
Name string `sql:"type:varchar(255);not null;"`
}
)
func TestMultipleSingularTableInvocations(t *testing.T) {
DB.SingularTable(true) // Invocation #1.
// DB.LogMode(true)
entities := []interface{}{
&Organization{},
&App{},
}
for i := len(entities) - 1; i >= 0; i-- {
if err := DB.DropTableIfExists(entities[i]).Error; err != nil {
t.Fatalf("Drop table for entity=%+v: %s", entities[i], err)
}
}
if err := DB.AutoMigrate(entities...).Error; err != nil {
t.Fatalf("Auto-migrate failed for entity=%+v: %s", entity, err)
}
if err := DB.Model(&App{}).AddForeignKey("organization_id", "organization(id)", "RESTRICT", "RESTRICT"); err != nil {
t.Fatalf("Problem adding OrganizationId foreign-key to App table: %s", err)
}
createFixtures(t)
}
func createFixtures(t *testing.T) {
DB.SingularTable(true) // Invocation #2. If this were to clobber internal gorm state it can break things.
org := &Organization{
Name: "Some Organization for Testing",
}
if err := DB.Save(org).Error; err != nil {
t.Fatal(err)
}
app := &App{
OrganizationId: org.Id,
Name: "my-app-for-test-purposes",
}
if err := DB.Save(app).Error; err != nil {
t.Fatal(err)
}
}