diff --git a/database/database.go b/database/database.go index 422a6de..8dd1c6c 100644 --- a/database/database.go +++ b/database/database.go @@ -45,7 +45,6 @@ func (db *Database) AddTable(table *Table) error { if db.ExistedTable(name) { return fmt.Errorf("table %s already existed", name) } - table.dbClient = db.dbClient db.pendingTables[name] = table return nil } diff --git a/database/database_test.go b/database/database_test.go index 9d760fd..a2f259a 100644 --- a/database/database_test.go +++ b/database/database_test.go @@ -7,7 +7,7 @@ func TestNewDatabaseAndAddNewTable(t *testing.T) { if err != nil { t.Fatal(err) } - table, err := NewTableBuilder("test_table").AddFieldInt("test_field_int").AddFieldVarchar("test_field_string").Submit() + table, err := NewTableBuilder("test_table", db.dbClient).AddFieldInt("test_field_int").AddFieldVarchar("test_field_string").Submit() if err != nil { t.Fatal(err) } diff --git a/database/table.go b/database/table.go index 32840ed..9751e1b 100644 --- a/database/table.go +++ b/database/table.go @@ -14,10 +14,11 @@ type Table struct { Fields map[string]*Field } -func NewTable(name string) *Table { +func NewTable(name string, dbClient *gorm.DB) *Table { return &Table{ - name: name, - Fields: map[string]*Field{}, + name: name, + Fields: map[string]*Field{}, + dbClient: dbClient, } } @@ -90,9 +91,9 @@ type TableBuilder struct { table *Table } -func NewTableBuilder(name string) *TableBuilder { +func NewTableBuilder(name string, dbClient *gorm.DB) *TableBuilder { return &TableBuilder{ - table: NewTable(name), + table: NewTable(name, dbClient), } } diff --git a/database/table_test.go b/database/table_test.go index c9fc3f3..32e6d4e 100644 --- a/database/table_test.go +++ b/database/table_test.go @@ -3,7 +3,7 @@ package database import "testing" func TestNewTable(t *testing.T) { - table, err := NewTableBuilder("test_table_name").AddFieldInt("test_field_int").Submit() + table, err := NewTableBuilder("test_table_name", nil).AddFieldInt("test_field_int").Submit() if err != nil { t.Fatal(err) }