Your Question
When I passed an object assigned 10 to its primary key ID, the sql generated is not added condition.
Code:
type User struct {
ID uint `gorm:"primaryKey"`
Name string
Email string
Birthday *time.Time
}
func testSelect() {
user = User{}
db.Debug().Model(User{ID: 10}).First(&user)
fmt.Println(user)
}
Answer:
2023/11/23 20:37:27 /Users/zenkiebear/Workspace/Golang/learn-go/10.gorm/main.go:163
[0.170ms] [rows:1] SELECT * FROM `user` ORDER BY `user`.`id` LIMIT 1
{1 John Doe doe@zenkie.cn 2023-11-23 00:00:00 +0800 CST}
The document you expected this should be explained
Expected answer
The SQL should be select * from user where id = 10 order by user.id limit 1
I learned this from our tutorial document, and my code is almost consistent with the sample.