go - Gorm执行SELECT * FROM表名,但是它也执行WHERE “tablename” .“id” = 0;找不到记录返回

标签 go go-gorm

enter image description here我正在尝试在Go中创建一个简单的Blog API。
这是我的结构

type Blog struct {
    Id     int    `gorm:"primary key" json:"id"`
    Author string `gorm:"type:varchar(100);not null" json:"author"`
    Title  string `gorm:"type:varchar(1000);not null" json:"title"`
    Body   string `gorm:"size:1000;not null" json:"body"`
}
除get all方法外,所有其他create get方法均起作用

func (b *Blog) GetAll(db *gorm.DB) (*[]Blog, error){
    var blogs  []Blog
    records := db.Find(&blogs)
    if records.Error != nil{
        return &[]Blog{}, records.Error
    }
    return &blogs, nil
}
此方法执行此查询,如调试输出所示
SELECT * FROM "blogs" WHERE "blogs"."id" = 0 ORDER BY "blogs"."id" LIMIT 1

显然不会返回任何东西
我曾尝试在文档上查找,但是文档建议我只这样做……而在stackoverflow上没有人遇到过此问题

最佳答案

好吧,我发现出了什么问题
enter image description here
这些是我的路线
因为路由/blog/{id}是在我向/get/all发出get请求时首先定义的
它会默认为/blog/id并将其全部作为id,并且当它转换为int时会出错,因此使用id = 0却不给我任何东西
所以要解决这个问题,我只需要更改路线
像这样 :
enter image description here
我希望有人能从我的愚蠢错误中学到东西:>

关于go - Gorm执行SELECT * FROM表名,但是它也执行WHERE “tablename” .“id” = 0;找不到记录返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65109294/

相关文章:

go - 向 exec.Cmd.StdinPipe() 写入超过 4K 字节

go - 如何查看 Go 项目的大小?

mysql - 如何将 `where in` 与整数片段一起使用

go - 返回 nil 结构而不是空结构 go-gorm

go - 如何将 gorm.DB 查询转换为其字符串表示形式

Go迁移不会创建外键

golang : how to simulate union type efficiently

amazon-web-services - 使用AWS Go SDK创建客户端时出现UnrecognizedClientException

amazon-web-services - 使用 aws-sdk-go/roleARN/queueARN (sqs) 获取消息

mysql - 结构用户 - 有很多 session 。查找给定 session 的用户