Your Question

When using Find with a value which is of type any or interface{}, the slice does not get filled with the result. See example below.

type MyModel struct {
    ID   int64 `gorm:"primaryKey"`
    Name string
}

// This works
func fetchAll() ([]MyModel, error) {
    var result []MyModel
    if err := db.Find(&result).Error; err != nil {
        return nil, err
    }
    return result, nil
}

// This does not work
func fetchAll() ([]MyModel, error) {
    var result any
    result = []MyModel{}
    if err := db.Find(&result).Error; err != nil {
        return nil, err
    }
    return result, nil
}

There are no errors and no indication what is going wrong. My real use case is a bit more complicated, but this the minimal reproducable example.

The document you expected this should be explained

https://gorm.io/docs/query.html

Expected answer

Either an error or that it fills the slice as expected.

Comment From: github-actions[bot]

This issue has been automatically marked as stale because it has been open 360 days with no activity. Remove stale label or comment or this will be closed in 180 days