Skip to content

Instantly share code, notes, and snippets.

@mymedia2

mymedia2/main.go Secret

Created November 9, 2022 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mymedia2/9e809d210c68ab5107f0bce3d28587b8 to your computer and use it in GitHub Desktop.
Save mymedia2/9e809d210c68ab5107f0bce3d28587b8 to your computer and use it in GitHub Desktop.
Incorrect parenthesis?
package main
import (
"time"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func PaidWithCreditCard(db *gorm.DB) *gorm.DB {
return db.Where("pay_mode = ?", "card")
}
func main() {
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
db.AutoMigrate(&Order{})
thisYear := time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Local)
var orders []Order
db.Debug().Scopes(PaidWithCreditCard).
Where("product = ?", "banana").Or("created_at > ?", thisYear).
Find(&orders)
_ = err // we ignore errors for simplicity
}
type Order struct {
gorm.Model
Product string
PayMode string
}
2022/11/09 18:15:18 /home/mymedia/.go/pkg/mod/gorm.io/gorm@v1.24.1/callbacks.go:134
[0.073ms] [rows:0] SELECT * FROM `orders` WHERE (product = "banana" OR created_at > "2022-01-01 00:00:00" AND pay_mode = "card") AND `orders`.`deleted_at` IS NULL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment