Go 正在数据库中获取所有项

Go 正在数据库中获取所有项,go,go-gorm,Go,Go Gorm,我正在尝试使用gorm从数据库中获取一个项目数组。它工作正常,我能够检索数据,但我只在数组中获取一个项目,而数组中的项目不止这些。这是我检索数组的代码 型号。开始 type Address struct { ID uuid.UUID `json:"id" gorm:"primary_key;unique;not null"` Line string `gorm:"size:255;not null;column:line" json:"line"

我正在尝试使用
gorm
从数据库中获取一个项目数组。它工作正常,我能够检索数据,但我只在数组中获取一个项目,而数组中的项目不止这些。这是我检索数组的代码

型号。开始

type Address struct {
    ID        uuid.UUID  `json:"id" gorm:"primary_key;unique;not null"`
    Line      string     `gorm:"size:255;not null;column:line" json:"line"`
    Hostel    string     `gorm:"size:255;not null;column:hostel" json:"hostel"`
    CreatedAt time.Time  `gorm:"default:CURRENT_TIMESTAMP;column:createdAt" json:"createdAt"`
    UpdatedAt time.Time  `gorm:"default:CURRENT_TIMESTAMP;column:updatedAt" json:"updatedAt"`
    DeletedAt *time.Time `sql:"index;column:deletedAt" json:"deletedAt"`
}

type Addresses []Address
type DbAddress struct {
    *model.Address
}

func (address *DbAddress) Get(db *gorm.DB) ([]model.Address, *errors.Error) {
    var err error
    addr := []model.Address{}
    err = db.Debug().Model(model.Address{}).Limit(100).Take(&addr).Error
    if gorm.IsRecordNotFoundError(err) {
        return nil, errors.NewNotFoundError(fmt.Sprintf("Address not found: %s", err.Error()))
    }
    if err != nil {
        return nil, errors.NewBadRequestError(fmt.Sprintf("error when trying to get Address: %s", err.Error()))
    }
    return addr, nil
}
func GetAddress() (model.Addresses, *errors.Error) {
    result := database.DbAddress{Address: &model.Address{}}

    return result.Get(database.DB)
}
func GetAddress(c *gin.Context) {

    address, getErr := services.GetAddress()
    if getErr != nil {
        res.ResponseJSON(c, getErr.Status, nil, true, getErr.Message)
        return
    }
    res.ResponseJSON(c, http.StatusFound, address, false, "successfully got address data")
}
数据库。转到

type Address struct {
    ID        uuid.UUID  `json:"id" gorm:"primary_key;unique;not null"`
    Line      string     `gorm:"size:255;not null;column:line" json:"line"`
    Hostel    string     `gorm:"size:255;not null;column:hostel" json:"hostel"`
    CreatedAt time.Time  `gorm:"default:CURRENT_TIMESTAMP;column:createdAt" json:"createdAt"`
    UpdatedAt time.Time  `gorm:"default:CURRENT_TIMESTAMP;column:updatedAt" json:"updatedAt"`
    DeletedAt *time.Time `sql:"index;column:deletedAt" json:"deletedAt"`
}

type Addresses []Address
type DbAddress struct {
    *model.Address
}

func (address *DbAddress) Get(db *gorm.DB) ([]model.Address, *errors.Error) {
    var err error
    addr := []model.Address{}
    err = db.Debug().Model(model.Address{}).Limit(100).Take(&addr).Error
    if gorm.IsRecordNotFoundError(err) {
        return nil, errors.NewNotFoundError(fmt.Sprintf("Address not found: %s", err.Error()))
    }
    if err != nil {
        return nil, errors.NewBadRequestError(fmt.Sprintf("error when trying to get Address: %s", err.Error()))
    }
    return addr, nil
}
func GetAddress() (model.Addresses, *errors.Error) {
    result := database.DbAddress{Address: &model.Address{}}

    return result.Get(database.DB)
}
func GetAddress(c *gin.Context) {

    address, getErr := services.GetAddress()
    if getErr != nil {
        res.ResponseJSON(c, getErr.Status, nil, true, getErr.Message)
        return
    }
    res.ResponseJSON(c, http.StatusFound, address, false, "successfully got address data")
}
服务。开始

type Address struct {
    ID        uuid.UUID  `json:"id" gorm:"primary_key;unique;not null"`
    Line      string     `gorm:"size:255;not null;column:line" json:"line"`
    Hostel    string     `gorm:"size:255;not null;column:hostel" json:"hostel"`
    CreatedAt time.Time  `gorm:"default:CURRENT_TIMESTAMP;column:createdAt" json:"createdAt"`
    UpdatedAt time.Time  `gorm:"default:CURRENT_TIMESTAMP;column:updatedAt" json:"updatedAt"`
    DeletedAt *time.Time `sql:"index;column:deletedAt" json:"deletedAt"`
}

type Addresses []Address
type DbAddress struct {
    *model.Address
}

func (address *DbAddress) Get(db *gorm.DB) ([]model.Address, *errors.Error) {
    var err error
    addr := []model.Address{}
    err = db.Debug().Model(model.Address{}).Limit(100).Take(&addr).Error
    if gorm.IsRecordNotFoundError(err) {
        return nil, errors.NewNotFoundError(fmt.Sprintf("Address not found: %s", err.Error()))
    }
    if err != nil {
        return nil, errors.NewBadRequestError(fmt.Sprintf("error when trying to get Address: %s", err.Error()))
    }
    return addr, nil
}
func GetAddress() (model.Addresses, *errors.Error) {
    result := database.DbAddress{Address: &model.Address{}}

    return result.Get(database.DB)
}
func GetAddress(c *gin.Context) {

    address, getErr := services.GetAddress()
    if getErr != nil {
        res.ResponseJSON(c, getErr.Status, nil, true, getErr.Message)
        return
    }
    res.ResponseJSON(c, http.StatusFound, address, false, "successfully got address data")
}
控制器。开始

type Address struct {
    ID        uuid.UUID  `json:"id" gorm:"primary_key;unique;not null"`
    Line      string     `gorm:"size:255;not null;column:line" json:"line"`
    Hostel    string     `gorm:"size:255;not null;column:hostel" json:"hostel"`
    CreatedAt time.Time  `gorm:"default:CURRENT_TIMESTAMP;column:createdAt" json:"createdAt"`
    UpdatedAt time.Time  `gorm:"default:CURRENT_TIMESTAMP;column:updatedAt" json:"updatedAt"`
    DeletedAt *time.Time `sql:"index;column:deletedAt" json:"deletedAt"`
}

type Addresses []Address
type DbAddress struct {
    *model.Address
}

func (address *DbAddress) Get(db *gorm.DB) ([]model.Address, *errors.Error) {
    var err error
    addr := []model.Address{}
    err = db.Debug().Model(model.Address{}).Limit(100).Take(&addr).Error
    if gorm.IsRecordNotFoundError(err) {
        return nil, errors.NewNotFoundError(fmt.Sprintf("Address not found: %s", err.Error()))
    }
    if err != nil {
        return nil, errors.NewBadRequestError(fmt.Sprintf("error when trying to get Address: %s", err.Error()))
    }
    return addr, nil
}
func GetAddress() (model.Addresses, *errors.Error) {
    result := database.DbAddress{Address: &model.Address{}}

    return result.Get(database.DB)
}
func GetAddress(c *gin.Context) {

    address, getErr := services.GetAddress()
    if getErr != nil {
        res.ResponseJSON(c, getErr.Status, nil, true, getErr.Message)
        return
    }
    res.ResponseJSON(c, http.StatusFound, address, false, "successfully got address data")
}
使用而不是


有关更多详细信息,请参见:

使用
Find
而不是
Take
@mkopriva,我想如果您将您的评论作为答案会有所帮助。然后,金可以接受它作为正确的反应,就像你希望的那样。