Gorm: Issue with Find() and First()

Created on 22 Nov 2014  ·  3Comments  ·  Source: go-gorm/gorm

I'm not sure if this problem is with your code or documentation.
I also assume you designed GORM based on Ruby On Rails or PHP-Laravel which is why I believe there is a mistake.

http://laravel.com/docs/4.2/eloquent

I don't expect you to agree with all of my suggestions because your trying to make Find() and First() accomplish what 4 commands in Laravel: All(), First(), Find(), Get().

But the downside is that your method ALWAYS returns slice even if it is more convenient to get One Struct (without slice) when you WANT and EXPECT one result.

Most helpful comment

Hello @pjebs

Although I stole the ideas from ActiveRecord, it doesn't works exactly like it, but works like below:

db.First(&User{})   // first user, sort by primary key
db.First(&[]User{}) // find the first user, and put it into the array

db.Find(&User{})   // first user
db.Find(&[]User{}) // find all users

// find records with conditions
db.First(&User{}, 123)
db.First(&User{}, "name = ?", "hello")
db.Find(&User{}, "name = ?", "hello")
db.Where("name = ?", "hello").First(&User{})
db.Where("name = ?", "hello").Find(&User{})

All 3 comments

type Customer struct {
Id uint32 gorm:"column:id; primary_key:yes"
Access_token string gorm:"column:access_token"
}

func (c Customer) TableName() string {
return "customers"
}

func MyMiddleware(rw http.ResponseWriter, r *http.Request) {
// u := []Customer{}
u := &Customer{}

db, _ := gorm.Open("mysql", "root:@/main")
// db.First(&u, 5)
// db.First(&u)
// db.Find(&u, 5)
db.Find(&u)

}

This is the problem:

FIRST COMMAND:
This should return the first result back from the collection of all results being returned. (whether there are extra WHERE parameters or not).
It should parse the ONE result into &Customer{} and NOT have an array with one result in there.

Currently it does not work when &u is not a reference to a slice. &u should point to &Customer{} and not &[]Customer{}.

This function should also NEVER accept an extra parameter for the primary key. That is what the FIND function is for.

FIND COMMAND:
When Find command is given a primary key, it should not return a slice. It should also ALWAYS expect a primary key value.

If you want all values, then use "ALL" COMMAND. db.ALL(&u) where &u=[]Customer{}

http://daylerees.com/codebright/eloquent-queries
http://laravel.com/docs/4.2/queries

ALL COMMAND:
This should be included in your library. That would return all values.

db.All(&u), where &u=[]Customer{}

GET COMMAND:
This should be included in your library. This would return an ARRAY of all values including after you put WHERE constraints on.

Hello @pjebs

Although I stole the ideas from ActiveRecord, it doesn't works exactly like it, but works like below:

db.First(&User{})   // first user, sort by primary key
db.First(&[]User{}) // find the first user, and put it into the array

db.Find(&User{})   // first user
db.Find(&[]User{}) // find all users

// find records with conditions
db.First(&User{}, 123)
db.First(&User{}, "name = ?", "hello")
db.Find(&User{}, "name = ?", "hello")
db.Where("name = ?", "hello").First(&User{})
db.Where("name = ?", "hello").Find(&User{})
Was this page helpful?
0 / 5 - 0 ratings

Related issues

satb picture satb  ·  3Comments

hypertornado picture hypertornado  ·  3Comments

rfyiamcool picture rfyiamcool  ·  3Comments

corvinusy picture corvinusy  ·  3Comments

fieryorc picture fieryorc  ·  3Comments