Description

According to the documentation :

I want to test run the same test as explain in the Example 1:

db, _ := gorm.Open(mysql.Open(os.Getenv("RDS_USERNAME") + ":" + os.Getenv("RDS_PASSWORD") + "@tcp(" + os.Getenv("RDS_HOSTNAME") + ":" + os.Getenv("RDS_PORT") + ")/" + os.Getenv("RDS_DB_NAME") + "?charset=utf8&parseTime=True&loc=UTC"), &gorm.Config{})
db = db.Debug()

var user model.User
r := db.Where("firstName = ?", "Mathieu").Last(&user)
if r.Error != nil {
    fmt.Println(r.Error)
} else {
    fmt.Println(user.ID)
}

r = db.Where("firstName = ?", "Thomas").Last(&user)
if r.Error != nil {
    fmt.Println(r.Error)
} else {
    fmt.Println(user.ID)
}

db.Find(&user)

This code generated the following queries :

[25.228ms] [rows:1] SELECT * FROM `users` WHERE firstName = 'Mathieu' ORDER BY `users`.`id` DESC LIMIT 1
[24.019ms] [rows:0] SELECT * FROM `users` WHERE firstName = 'Thomas' AND `users`.`id` = 20310 ORDER BY `users`.`id` DESC LIMIT 1
[24.181ms] [rows:1] SELECT * FROM `users` WHERE `users`.`id` = 20310

Or, according to the documentation Last is a finisher method the the queries should be :+1:

[25.228ms] [rows:1] SELECT * FROM `users` WHERE firstName = 'Mathieu' ORDER BY `users`.`id` DESC LIMIT 1
[24.019ms] [rows:0] SELECT * FROM `users` WHERE firstName = 'Thomas' ORDER BY `users`.`id` DESC LIMIT 1
[24.181ms] [rows:1] SELECT * FROM `users`

Did I forgot something ou the documentation is not up to date ?

Thank's

Comment From: github-actions[bot]

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

Comment From: black-06

You may have noticed that they all have users.id = 20310.

Look here Retrieving-objects-with-primary-key

When the destination object has a primary value, the primary key will be used to build the condition, for example: go var user = User{ID: 10} db.First(&user) // SELECT * FROM users WHERE id = 10;

After the first query, your user already has the primary key value. So it's used for later queries.

Comment From: github-actions[bot]

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