Description
I am encountering an issue where consecutive calls to the First method generate a SQL query with duplicated conditions. This seems to happen under specific circumstances described below.
Steps to Reproduce
- Perform a First call with an integer parameter:
db.First(&user, 1)
log.Println(user)
- Immediately after, perform another First call with a string parameter:
db.First(&user, "1")
log.Println(user)
- Observe the generated SQL for the second call.
Expected Behavior
I expect each call to First to generate an independent SQL query, specifically: The first call should generate:
SELECT * FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` LIMIT 1
The second call should generate:
SELECT * FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` LIMIT 1
Actual Behavior
The second First call generates a SQL query with duplicated conditions:
SELECT * FROM `users` WHERE `users`.`id` = 1 AND `users`.`id` = 1 ORDER BY `users`.`id` LIMIT 1
Additional Context
- GORM: 1.25.9
- MySql: 8.0.36 for Win64 on x86_64
- Go: 1.20.6
Potential Causes
I suspect there might be a bug in how GORM handles or caches query parameters, especially when different types of parameters are used consecutively. It's unclear why the condition would duplicate in the query builder.
Request
I would appreciate any insights or suggestions on whether this is expected behavior under certain conditions or if it's indeed an issue that needs addressing. Thank you for your assistance!
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: ivila
Maybe is work as expected?
add one line between the two calls will solve your Immediately after problem:
user.ID = 0
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: solisamicus
Maybe is work as expected? add one line between the two calls will solve your
Immediately afterproblem:
go user.ID = 0
db.First(&user, 1)
log.Println(user)
user.ID = 0
db.First(&user, "1")
log.Println(user)
Indeed get the expected results!
Reason: When executing a query like db.First(&user, 1), the field ID of the user structure is filled with values retrieved from the database. If the same user instance is used again in consecutive queries without resetting its state, such as if user.ID has been set to a specific non-zero value, GORM may treat this ID value as an implicit query condition.
Thank you!
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 ✨