Gorm 执行 SELECT * FROM tablename 但它也执行 WHERE "tablename"."id"=0; returns 没有找到记录

Gorm executes SELECT * FROM tablename but it also does WHERE "tablename"."id"=0; Which returns no record found

我正在尝试用 Go 创建一个简单的博客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

这显然return什么都没有

我试过在文档上查找,但文档建议我只这样做...而且 Whosebug 上没有人遇到过这个问题

好的,我发现出了什么问题

这些是我的路线 因为如果我向 /get/all 发出获取请求,那么路由 /blog/{id} 是首先定义的 它会默认为 /blog/id 并将所有内容作为 id,当它转换为 int 时会出错,因此使用 id = 0 并且什么都不给我

所以为了解决这个问题,我只需要改变我的路线

像这样:

我希望有人能从我的愚蠢错误中吸取教训:>