When I search for a nonexistent record, no error is returned, while I expect for ErrRecordNotFound
here is an example:
package main
import (
"errors"
"fmt"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"time"
)
var Db *gorm.DB
type VModel struct {
ID uint64 `gorm:"index:id; primarykey, autoIncrement"`
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time
DeletedAt gorm.DeletedAt
}
type Archive struct {
VModel
Path string `gorm:"index:path;type:varchar(100)"`
Password string `gorm:"type:varchar(10)"`
}
func main() {
dsn := "MYSQL_CONNECTION_STRING"
Db, _ = gorm.Open(mysql.Open(dsn), &gorm.Config{})
t := Archive{}
err := Db.Model(&Archive{}).Where("path = ?", "/root").Find(&t).Error
fmt.Println(errors.Is(err, gorm.ErrRecordNotFound))
fmt.Println(err)
fmt.Printf("%+v", t)
}
Here is the SQL that creates the table:
CREATE TABLE `archives` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`created_at` datetime(3) DEFAULT NULL,
`updated_at` datetime(3) DEFAULT NULL,
`deleted_at` datetime(3) DEFAULT NULL,
`path` varchar(100) DEFAULT NULL,
`password` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `id` (`id`),
KEY `path` (`path`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
And you do not have to insert any data into the table, just run the code above and the output will be like following:
false
<nil>
{VModel:{ID:0 CreatedAt:0001-01-01 00:00:00 +0000 UTC UpdatedAt:0001-01-01 00:00:00 +0000 UTC DeletedAt:{Time:0001-01-01 00:00:00 +0000 UTC Valid:false}} Path: Password:}
On the one hand, no error is returned, on the other hand, the returned t is an empty struct. Is there anything I missed?
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
Same as #6105
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: programmerX1123
Same as #6105
Thank you