Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecordNotFound returns false when there are no rows

Tags:

go

go-gorm

I am having a problem with this library because this function returns false even when the given input is not in the database, when in fact it should return true.

type User struct {
    ID          uint      `gorm:"primary_key"`
    Username    string    `json:",omitempty"`
    Password    string    `json:",omitempty"`
    CreatedAt   time.Time `json:",omitempty"`
}

b, err := db.Con()
if err != nil {
    log.Panic(err)
}

defer db.Close()

// We want an empty struct
// Otherwise it will trigger the unique key constraint
user := []User{}

// Check if the username is taken
// BUX, MUST FIX: This always returns false for some reason
if db.Where(&User{Username: "MyUsername"}).Find(&user).RecordNotFound() == false {
    fmt.Println("Username found")
}

Why is it always returning false, even when the string is empty?

like image 532
Jeffrey Anderson Avatar asked Aug 28 '18 21:08

Jeffrey Anderson


People also ask

Can you return NULL if a record does not exist?

So return nullif it is can happen in your domain, that records do not exist (in my experience this is most often the case). If you expect a record to exist and it is not there, then it is valid to throw an exception. Share Follow answered Aug 27 '16 at 19:40

What happens when you have no records in an array?

This has many advantages including that when you have no records it is an empty array. So when you have records, you would be returning: And when you have no records, you would be returning: Show activity on this post.

Is there a function to catch no row in a query?

@chrismarx: If your query returns no row (nothing at all), there is no place for a function to catch that. So we need an outer SELECT - and then we don't even need a function. This works in DB2 as well, provided you substitute DUAL with SYSIBM.SYSDUMMY1. To make it more simplier, this should work fine.

What happens if you return null when throwing an exception?

If you return nullthen it will be a normal and 'non exceptional' state in your model. So return nullif it is can happen in your domain, that records do not exist (in my experience this is most often the case). If you expect a record to exist and it is not there, then it is valid to throw an exception.


2 Answers

Looks like .RecordNotFound() is removed from the SDK for some reason.

Now use this for handling record not found errors

dbRresult := userHandler.db.Where("email = ?", email).First(&user)
if errors.Is(dbRresult.Error, gorm.ErrRecordNotFound) {
    // handle record not found
}
like image 168
hushed_voice Avatar answered Sep 28 '22 00:09

hushed_voice


The following code should work as you expect it:

// We want an empty struct
user := User{} // We expect to have one (or no) user returned.

// Check if the username is taken
// Notice the use of First() instead of Find()
if !db.Where("username = ?", "MyUsername").First(&user).RecordNotFound() {
    fmt.Println("Username found, here's the user:", user)
} else {
    fmt.Println("Username not found")
}

As mkopriva already mentioned the ErrRecordNotFound will not trigger when you're working with slices.

Since you don't need slices (your username should be unique) we can:

  1. Reference not a slice of Users but a single User User{} instead of []User{}.

  2. Use gorms First() method instead of Find().

like image 35
Mihailo Avatar answered Sep 27 '22 23:09

Mihailo