gorm
gorm copied to clipboard
*gorm.DB.Order() When this function is executed, the modified statement is shared across multiple instances instead of copying the new statement.
GORM Playground Link
https://github.com/masx200/playground/tree/master
Description
*gorm.DB.Order() When this function is executed, the modified statement is shared across multiple instances instead of copying the new statement.
https://github.com/masx200/playground/blob/master/main.go
https://github.com/masx200/playground/blob/master/log.txt
This may be caused by a failure to clone a "* gorm.DB" object.
error occurs in sql statement
SELECT `to_do_items`.`created_at`,`to_do_items`.`updated_at`,`to_do_items`.`content`,`to_do_items`.`completed`,`to_do_items`.`id`,`to_do_items`.`author` FROM `to_do_items` WHERE `to_do_items`.`deleted_at` IS NULL ORDER BY id DESC,id DESC,id DESC,id DESC,id DESC,completed ASC,completed ASC,completed ASC,completed ASC,completed ASC LIMIT 10
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question
template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
func main() {
dsn := "todolist:todolist@tcp(127.0.0.1:3306)/todolist?charset=utf8mb4&parseTime=True&loc=Local"
createDB := ConnectDatabase(dsn, &ToDoItem{}, "to_do_items", true)
for i := 0; i < 5; i++ {
res, err := FindItems(createDB, 10, 0, &ToDoItem{}, map[string]any{}, "id", "desc")
if err != nil {
panic(err)
}
fmt.Printf("%v", len(res))
}
for i := 0; i < 5; i++ {
res, err := FindItems(createDB, 10, 0, &ToDoItem{}, map[string]any{}, "completed", "asc")
if err != nil {
panic(err)
}
fmt.Printf("%v", len(res))
}
}
func FindItems[T any](createDB /* func() */*gorm.DB, limit int, page int, model *T, query map[string]any, order string, direction string) ([]map[string]any, error) {
db := createDB/* () */
// sqlDB, err := db.DB()
// if err != nil {
// panic(err)
// }
// defer sqlDB.Close()
if direction == "desc" {
db = db.Order(order + " " + "DESC")
} else {
db = db.Order(order + " " + "ASC")
}
var items = []map[string]any{}
result := db.Model(model).Omit("deleted_at").Where(query).Limit(limit).Offset(page * limit).Find(&items)
return items, result.Error
}
2022/10/27 21:25:32 C:/Documents/test/gorm-test/main.go:26
[0.927ms] [rows:10] SELECT `to_do_items`.`created_at`,`to_do_items`.`updated_at`,`to_do_items`.`content`,`to_do_items`.`completed`,`to_do_items`.`id`,`to_do_items`.`author` FROM `to_do_items` WHERE `to_do_items`.`deleted_at` IS NULL ORDER BY id DESC LIMIT 10
10
2022/10/27 21:25:32 C:/Documents/test/gorm-test/main.go:26
[0.759ms] [rows:10] SELECT `to_do_items`.`created_at`,`to_do_items`.`updated_at`,`to_do_items`.`content`,`to_do_items`.`completed`,`to_do_items`.`id`,`to_do_items`.`author` FROM `to_do_items` WHERE `to_do_items`.`deleted_at` IS NULL ORDER BY id DESC,id DESC LIMIT 1010
2022/10/27 21:25:32 C:/Documents/test/gorm-test/main.go:26
[0.567ms] [rows:10] SELECT `to_do_items`.`created_at`,`to_do_items`.`updated_at`,`to_do_items`.`content`,`to_do_items`.`completed`,`to_do_items`.`id`,`to_do_items`.`author` FROM `to_do_items` WHERE `to_do_items`.`deleted_at` IS NULL ORDER BY id DESC,id DESC,id DESC LIMIT 10
10
2022/10/27 21:25:32 C:/Documents/test/gorm-test/main.go:26
[0.765ms] [rows:10] SELECT `to_do_items`.`created_at`,`to_do_items`.`updated_at`,`to_do_items`.`content`,`to_do_items`.`completed`,`to_do_items`.`id`,`to_do_items`.`author` FROM `to_do_items` WHERE `to_do_items`.`deleted_at` IS NULL ORDER BY id DESC,id DESC,id DESC,id DESC LIMIT 10
10
2022/10/27 21:25:32 C:/Documents/test/gorm-test/main.go:26
[0.735ms] [rows:10] SELECT `to_do_items`.`created_at`,`to_do_items`.`updated_at`,`to_do_items`.`content`,`to_do_items`.`completed`,`to_do_items`.`id`,`to_do_items`.`author` FROM `to_do_items` WHERE `to_do_items`.`deleted_at` IS NULL ORDER BY id DESC,id DESC,id DESC,id DESC,id DESC LIMIT 10
10
2022/10/27 21:25:32 C:/Documents/test/gorm-test/main.go:26
[3.653ms] [rows:10] SELECT `to_do_items`.`created_at`,`to_do_items`.`updated_at`,`to_do_items`.`content`,`to_do_items`.`completed`,`to_do_items`.`id`,`to_do_items`.`author` FROM `to_do_items` WHERE `to_do_items`.`deleted_at` IS NULL ORDER BY id DESC,id DESC,id DESC,id DESC,id DESC,completed ASC LIMIT 10
10
2022/10/27 21:25:32 C:/Documents/test/gorm-test/main.go:26
[2.981ms] [rows:10] SELECT `to_do_items`.`created_at`,`to_do_items`.`updated_at`,`to_do_items`.`content`,`to_do_items`.`completed`,`to_do_items`.`id`,`to_do_items`.`author` FROM `to_do_items` WHERE `to_do_items`.`deleted_at` IS NULL ORDER BY id DESC,id DESC,id DESC,id DESC,id DESC,completed ASC,completed ASC LIMIT 10
10
2022/10/27 21:25:32 C:/Documents/test/gorm-test/main.go:26
[2.828ms] [rows:10] SELECT `to_do_items`.`created_at`,`to_do_items`.`updated_at`,`to_do_items`.`content`,`to_do_items`.`completed`,`to_do_items`.`id`,`to_do_items`.`author` FROM `to_do_items` WHERE `to_do_items`.`deleted_at` IS NULL ORDER BY id DESC,id DESC,id DESC,id DESC,id DESC,completed ASC,completed ASC,completed ASC LIMIT 10
10
2022/10/27 21:25:32 C:/Documents/test/gorm-test/main.go:26
[3.757ms] [rows:10] SELECT `to_do_items`.`created_at`,`to_do_items`.`updated_at`,`to_do_items`.`content`,`to_do_items`.`completed`,`to_do_items`.`id`,`to_do_items`.`author` FROM `to_do_items` WHERE `to_do_items`.`deleted_at` IS NULL ORDER BY id DESC,id DESC,id DESC,id DESC,id DESC,completed ASC,completed ASC,completed ASC,completed ASC LIMIT 10
10
2022/10/27 21:25:32 C:/Documents/test/gorm-test/main.go:26
[3.633ms] [rows:10] SELECT `to_do_items`.`created_at`,`to_do_items`.`updated_at`,`to_do_items`.`content`,`to_do_items`.`completed`,`to_do_items`.`id`,`to_do_items`.`author` FROM `to_do_items` WHERE `to_do_items`.`deleted_at` IS NULL ORDER BY id DESC,id DESC,id DESC,id DESC,id DESC,completed ASC,completed ASC,completed ASC,completed ASC,completed ASC LIMIT 10
10
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question
template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
https://gorm.io/docs/method_chaining.html#New-Session-Method