#7436·gorm

Proposal: Add Conditional WhereIf Method for Chainable Query Building

Author: chengkz2023Created Apr 24, 2025Updated Jul 27, 2026
Labelsstatus:staletype:feature_request

Describe the feature

Add a new chainable method with this signature: func (db *DB) WhereIf(condition bool, query interface{}, args ...interface{}) *DB Expected Behavior: When condition == true: Behaves exactly like existing Where() method When condition == false: No-op that returns the original *DB instance unchanged Usage Example: ··· db.Model(&User{}). WhereIf(age > 18, "age > ?", age). WhereIf(name != "", "name = ?", name). Find(&users) ···

Motivation

Currently, when building dynamic queries with GORM, we often need to write verbose conditional checks to decide whether to add a Where clause. For example: ··· query := db.Model(&User{}) if age > 18 { query = query.Where("age > ?", age) } if name != "" { query = query.Where("name = ?", name) } result := query.Find(&users) ···

Related Issues