From 7aaac3a580d5c0a4b28853c8f53d8feb0327530f Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 30 Jun 2020 23:06:48 +0800 Subject: [PATCH] Allow to use sql function in Group, Pluck --- chainable_api.go | 4 +++- finisher_api.go | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/chainable_api.go b/chainable_api.go index dbd783fd..e2ba44cc 100644 --- a/chainable_api.go +++ b/chainable_api.go @@ -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 } diff --git a/finisher_api.go b/finisher_api.go index 6d961811..af040106 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -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)