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

Cannot get gorm.ErrRecordNotFound when using pointer to slice #2014

Closed
easonlin404 opened this issue Aug 1, 2018 · 3 comments
Closed

Cannot get gorm.ErrRecordNotFound when using pointer to slice #2014

easonlin404 opened this issue Aug 1, 2018 · 3 comments

Comments

@easonlin404
Copy link
Contributor

easonlin404 commented Aug 1, 2018

I expect that can got gorm.ErrRecordNotFound If no record is found when using pointer to slice. And after trace code I noted that https://github.com/jinzhu/gorm/blob/master/callback_query.go#L87-L89 add isSlice bool value to block error setting.

So how can I got no database record found when I using pointer to slice? Thx.

} else if scope.db.RowsAffected == 0 && !isSlice {
				scope.Err(ErrRecordNotFound)
}

What version of Go are you using (go version)?

Latest

Which database and its version are you using?

sqlite

Please provide a complete runnable program to reproduce your issue. IMPORTANT

package main

import (
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/sqlite"
	"fmt"
)

type Product struct {
	gorm.Model
	Code string
	Price uint
}

func main() {
	db, _ := gorm.Open("sqlite3", "test.db")
	defer db.Close()

	// Migrate the schema
	db.AutoMigrate(&Product{})

	// Create
	db.Create(&Product{Code: "L1212", Price: 1000})

	var products []Product
	// No record can be found
	if err:= db.Where(map[string]interface{}{"code": ""}).Find(&products).Error;err!=nil {
		if err == gorm.ErrRecordNotFound {
			//expect getting ErrRecordNotFound error
			fmt.Print("ErrRecordNotFound")
		}
	}

}
@easonlin404
Copy link
Contributor Author

easonlin404 commented Aug 1, 2018

After I search issue I found https://github.com/jinzhu/gorm/issues/228#issuecomment-281573321 Find a slice won't return error, bug find a struct will.

Can add this to document or consider that make behavior consistency?

@gm42
Copy link
Contributor

gm42 commented Aug 7, 2018

This is totally unexpected; find on a slice should also return the error.

An example of how real code would look like right now:

        var originalRecords []*Record
	err := db.
		Joins("JOIN records ON records.ext_id = external.id").
		Where(`external."key" = ?`, key).
		Find(&originalRecords).Error
	if err != nil {
		return nil, err
	}
        if len(originalRecords) == 0 {
             return nil, gorm.ErrRecordNotFound
        }
        return originalRecords, nil

@easonlin404
Copy link
Contributor Author

Merged #2015 ,close it.

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

No branches or pull requests

2 participants