Allow to use sql function in Group, Pluck

This commit is contained in:
Jinzhu 2020-06-30 23:06:48 +08:00
parent 3e4dbde920
commit 7aaac3a580
2 changed files with 6 additions and 2 deletions

View File

@ -162,8 +162,10 @@ func (db *DB) Joins(query string, args ...interface{}) (tx *DB) {
// Group specify the group method on the find
func (db *DB) Group(name string) (tx *DB) {
tx = db.getInstance()
fields := strings.FieldsFunc(name, utils.IsChar)
tx.Statement.AddClause(clause.GroupBy{
Columns: []clause.Column{{Name: name}},
Columns: []clause.Column{{Name: name, Raw: len(fields) != 1}},
})
return
}

View File

@ -8,6 +8,7 @@ import (
"strings"
"gorm.io/gorm/clause"
"gorm.io/gorm/utils"
)
// Create insert the value into database
@ -325,9 +326,10 @@ func (db *DB) Pluck(column string, dest interface{}) (tx *DB) {
tx.AddError(ErrorModelValueRequired)
}
fields := strings.FieldsFunc(column, utils.IsChar)
tx.Statement.AddClauseIfNotExists(clause.Select{
Distinct: tx.Statement.Distinct,
Columns: []clause.Column{{Name: column}},
Columns: []clause.Column{{Name: column, Raw: len(fields) != 1}},
})
tx.Statement.Dest = dest
tx.callbacks.Query().Execute(tx)