我正在尝试在 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
这显然不会返回任何东西
我曾尝试在文档上查找,但文档建议我只这样做......并且stackoverflow上没有人遇到过这个问题
一只萌萌小番薯
相关分类