diff --git a/README.md b/README.md index d87b5715..27a5f3e0 100644 --- a/README.md +++ b/README.md @@ -710,6 +710,12 @@ db.Where("created_at > ?", "2013-10-10").Find(&cancelled_orders, "state = ?", "c //// SELECT * FROM orders WHERE created_at > '2013/10/10' AND state = 'shipped'; (shipped_orders) +// Use variable to keep query chain +todays_orders := db.Where("created_at > ?", "2013-10-29") +cancelled_orders := todays_orders.Where("state = ?", "cancelled") +shipped_orders := todays_orders.Where("state = ?", "shipped") + + // Search with shared conditions from different tables db.Where("product_name = ?", "fancy_product").Find(&orders).Find(&shopping_carts) //// SELECT * FROM orders WHERE product_name = 'fancy_product'; (orders) diff --git a/gorm_test.go b/gorm_test.go index db0e2fd7..38ec2b54 100644 --- a/gorm_test.go +++ b/gorm_test.go @@ -1354,11 +1354,14 @@ func TestTransaction(t *testing.T) { } } -func (s *CreditCard) BeforeSave() (err error) { - if s.Number == "0000" { - err = errors.New("invalid credit card") +func TestQueryChain(t *testing.T) { + var user_count1, user_count2 int64 + d := db.Model(User{}).Where("age > ?", 20) + d.Where("name = ?", "3").Count(&user_count1) + d.Count(&user_count2) + if user_count2 == user_count1 { + t.Error("DB object should be cloned when search") } - return } func BenchmarkGorm(b *testing.B) {