Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find with interface does not work #6796

Open
arnovanliere opened this issue Jan 16, 2024 · 1 comment
Open

Find with interface does not work #6796

arnovanliere opened this issue Jan 16, 2024 · 1 comment
Assignees
Labels

Comments

@arnovanliere
Copy link

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.

@arnovanliere arnovanliere added the type:question general questions label Jan 16, 2024
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants