fix reset colmap when counting distinct cols #2096

Merged
lunny merged 3 commits from finelog/xorm:fix_find_count_distinct_cols into master 2022-01-25 03:09:42 +00:00
2 changed files with 31 additions and 1 deletions

View File

@ -711,6 +711,36 @@ func TestFindAndCountWithGroupBy(t *testing.T) {
assert.EqualValues(t, 2, len(results))
}
func TestFindAndCountWithDistinct(t *testing.T) {
assert.NoError(t, PrepareEngine())
type FindAndCountWithDistinct struct {
Id int64
Age int `xorm:"index"`
Name string
}
assert.NoError(t, testEngine.Sync(new(FindAndCountWithDistinct)))
_, err := testEngine.Insert([]FindAndCountWithDistinct{
{
Name: "test1",
Age: 10,
},
{
Name: "test2",
Age: 20,
},
})
assert.NoError(t, err)
var results []FindAndCountWithDistinct
cnt, err := testEngine.Distinct("`age`").FindAndCount(&results)
assert.NoError(t, err)
assert.EqualValues(t, 2, cnt)
assert.EqualValues(t, 2, len(results))
}
type FindMapDevice struct {
Deviceid string `xorm:"pk"`
Status int

View File

@ -57,7 +57,7 @@ func (session *Session) FindAndCount(rowsSlicePtr interface{}, condiBean ...inte
if session.statement.SelectStr != "" {
session.statement.SelectStr = ""
}
if len(session.statement.ColumnMap) > 0 {
if len(session.statement.ColumnMap) > 0 && !session.statement.IsDistinct {
session.statement.ColumnMap = []string{}
}
if session.statement.OrderStr != "" {