Skip to content
This repository was archived by the owner on Sep 7, 2021. It is now read-only.
This repository is currently being migrated. It's locked while the migration is in progress.

Commit a491afa

Browse files
authored
fix tablename bug when sync2 (#921)
1 parent 9680df5 commit a491afa

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

session_find_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,3 +767,23 @@ func TestFindCacheLimit(t *testing.T) {
767767
assert.EqualValues(t, 1, len(beans2))
768768
}
769769
}
770+
771+
func TestFindJoin(t *testing.T) {
772+
type SceneItem struct {
773+
Type int
774+
DeviceId int64
775+
}
776+
777+
type DeviceUserPrivrels struct {
778+
UserId int64
779+
DeviceId int64
780+
}
781+
782+
assert.NoError(t, prepareEngine())
783+
assertSync(t, new(SceneItem), new(DeviceUserPrivrels))
784+
785+
var scenes []SceneItem
786+
err := testEngine.Join("LEFT OUTER", "device_user_privrels", "device_user_privrels.device_id=scene_item.device_id").
787+
Where("scene_item.type=?", 3).Or("device_user_privrels.user_id=?", 339).Find(&scenes)
788+
assert.NoError(t, err)
789+
}

session_schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func (session *Session) Sync2(beans ...interface{}) error {
248248
return err
249249
}
250250
structTables = append(structTables, table)
251-
tbName := session.tbNameNoSchema(table)
251+
tbName := engine.TableName(bean)
252252
tbNameWithSchema := engine.TableName(tbName, true)
253253

254254
var oriTable *core.Table

session_schema_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,41 @@ func (SyncTable2) TableName() string {
8282
return "sync_table1"
8383
}
8484

85+
type SyncTable3 struct {
86+
Id int64
87+
Name string `xorm:"unique"`
88+
Number string `xorm:"index"`
89+
Dev int
90+
Age int
91+
}
92+
93+
func (s *SyncTable3) TableName() string {
94+
return "sync_table1"
95+
}
96+
8597
func TestSyncTable(t *testing.T) {
8698
assert.NoError(t, prepareEngine())
8799

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

102+
tables, err := testEngine.DBMetas()
103+
assert.NoError(t, err)
104+
assert.EqualValues(t, 1, len(tables))
105+
assert.EqualValues(t, "sync_table1", tables[0].Name)
106+
90107
assert.NoError(t, testEngine.Sync2(new(SyncTable2)))
108+
109+
tables, err = testEngine.DBMetas()
110+
assert.NoError(t, err)
111+
assert.EqualValues(t, 1, len(tables))
112+
assert.EqualValues(t, "sync_table1", tables[0].Name)
113+
114+
assert.NoError(t, testEngine.Sync2(new(SyncTable3)))
115+
116+
tables, err = testEngine.DBMetas()
117+
assert.NoError(t, err)
118+
assert.EqualValues(t, 1, len(tables))
119+
assert.EqualValues(t, "sync_table1", tables[0].Name)
91120
}
92121

93122
func TestIsTableExist(t *testing.T) {

0 commit comments

Comments
 (0)