Your Question

Hello, we are using Method Chaining, and while it is powerful, we want to avoid problems when developers forget to use New Session. Is there a way, given a DB Handle, we can clear out the conditions that were added previously?

We tried starting a new session from a DB that had method chaining to understand if the NewSession would clear out the conditional statements, but it didn't.

db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{}) // db is a new initialized *gorm.DB, which falls under New Session Mode tx := db.Where("name = ?", "jinzhu") // Where("name = ?", "jinzhu") is the first method call, it creates a new Statement and adds conditions

tx.Where("age = ?", 18).Find(&users) // tx.Where("age = ?", 18) REUSES above Statement, and adds conditions to the Statement // Find(&users) is a finisher, it executes registered Query Callbacks, generates and runs the following SQL: // SELECT * FROM users WHERE name = 'jinzhu' AND age = 18

// NOW OVER HERE, given the db can we create a new session that doesn't have any of the conditions. Can we basically have a //pristine DB as if it was just opened, by calling some function?

The document you expected this should be explained

Expected answer

Comment From: a631807682

// db with empty statement
// db.clone is 1
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
// when db.clone is 1 
// tx(db1)  is new DB copy form db and now db.clone is 0
// tx(db1) with where clause
tx := db.Where("name = ?", "jinzhu")
// tx(db1) add other where clause
tx.Where("age = ?", 18)

So if you use chaining method and db.clone is 1, it will copy a new db from old with stmt. (like NewSession) Just var tx2 like tx2 := db.Where() it will return a new db with empty stmt what you need.