Skip to content

Commit

Permalink
fix tablename bug when sync2 (go-xorm#921)
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny authored May 2, 2018
1 parent 9680df5 commit a491afa
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
20 changes: 20 additions & 0 deletions session_find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -767,3 +767,23 @@ func TestFindCacheLimit(t *testing.T) {
assert.EqualValues(t, 1, len(beans2))
}
}

func TestFindJoin(t *testing.T) {
type SceneItem struct {
Type int
DeviceId int64
}

type DeviceUserPrivrels struct {
UserId int64
DeviceId int64
}

assert.NoError(t, prepareEngine())
assertSync(t, new(SceneItem), new(DeviceUserPrivrels))

var scenes []SceneItem
err := testEngine.Join("LEFT OUTER", "device_user_privrels", "device_user_privrels.device_id=scene_item.device_id").
Where("scene_item.type=?", 3).Or("device_user_privrels.user_id=?", 339).Find(&scenes)
assert.NoError(t, err)
}
2 changes: 1 addition & 1 deletion session_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (session *Session) Sync2(beans ...interface{}) error {
return err
}
structTables = append(structTables, table)
tbName := session.tbNameNoSchema(table)
tbName := engine.TableName(bean)
tbNameWithSchema := engine.TableName(tbName, true)

var oriTable *core.Table
Expand Down
29 changes: 29 additions & 0 deletions session_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,41 @@ func (SyncTable2) TableName() string {
return "sync_table1"
}

type SyncTable3 struct {
Id int64
Name string `xorm:"unique"`
Number string `xorm:"index"`
Dev int
Age int
}

func (s *SyncTable3) TableName() string {
return "sync_table1"
}

func TestSyncTable(t *testing.T) {
assert.NoError(t, prepareEngine())

assert.NoError(t, testEngine.Sync2(new(SyncTable1)))

tables, err := testEngine.DBMetas()
assert.NoError(t, err)
assert.EqualValues(t, 1, len(tables))
assert.EqualValues(t, "sync_table1", tables[0].Name)

assert.NoError(t, testEngine.Sync2(new(SyncTable2)))

tables, err = testEngine.DBMetas()
assert.NoError(t, err)
assert.EqualValues(t, 1, len(tables))
assert.EqualValues(t, "sync_table1", tables[0].Name)

assert.NoError(t, testEngine.Sync2(new(SyncTable3)))

tables, err = testEngine.DBMetas()
assert.NoError(t, err)
assert.EqualValues(t, 1, len(tables))
assert.EqualValues(t, "sync_table1", tables[0].Name)
}

func TestIsTableExist(t *testing.T) {
Expand Down

0 comments on commit a491afa

Please sign in to comment.