mirror of https://github.com/go-gorm/gorm.git
Fix count with join and no model, close #3255
This commit is contained in:
parent
ec82da396b
commit
045d5f8538
|
@ -96,7 +96,7 @@ func BuildQuerySQL(db *gorm.DB) {
|
||||||
|
|
||||||
// inline joins
|
// inline joins
|
||||||
if len(db.Statement.Joins) != 0 {
|
if len(db.Statement.Joins) != 0 {
|
||||||
if len(db.Statement.Selects) == 0 {
|
if len(db.Statement.Selects) == 0 && db.Statement.Schema != nil {
|
||||||
clauseSelect.Columns = make([]clause.Column, len(db.Statement.Schema.DBNames))
|
clauseSelect.Columns = make([]clause.Column, len(db.Statement.Schema.DBNames))
|
||||||
for idx, dbName := range db.Statement.Schema.DBNames {
|
for idx, dbName := range db.Statement.Schema.DBNames {
|
||||||
clauseSelect.Columns[idx] = clause.Column{Table: db.Statement.Table, Name: dbName}
|
clauseSelect.Columns[idx] = clause.Column{Table: db.Statement.Table, Name: dbName}
|
||||||
|
|
|
@ -67,4 +67,9 @@ func TestCount(t *testing.T) {
|
||||||
if !regexp.MustCompile(`SELECT COUNT\(DISTINCT\(.name.\)\) FROM .*users.*`).MatchString(result.Statement.SQL.String()) {
|
if !regexp.MustCompile(`SELECT COUNT\(DISTINCT\(.name.\)\) FROM .*users.*`).MatchString(result.Statement.SQL.String()) {
|
||||||
t.Fatalf("Build count with select, but got %v", result.Statement.SQL.String())
|
t.Fatalf("Build count with select, but got %v", result.Statement.SQL.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var count4 int64
|
||||||
|
if err := DB.Debug().Table("users").Joins("LEFT JOIN companies on companies.name = users.name").Where("users.name = ?", user1.Name).Count(&count4).Error; err != nil || count4 != 1 {
|
||||||
|
t.Errorf("count with join, got error: %v, count %v", err, count)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue