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

db.NewRecord() return false when I insert something first time #1044

Closed
polyzy opened this issue May 27, 2016 · 4 comments
Closed

db.NewRecord() return false when I insert something first time #1044

polyzy opened this issue May 27, 2016 · 4 comments

Comments

@polyzy
Copy link

polyzy commented May 27, 2016

type Ip_distribution struct {
    Ip          string `gorm:"primary_key"`
}
type Ip struct {
    Ip     string
}

func (i *Ip_distribution) TableName() string {
    return "ip_distribution"
}

func main() {
    db, _ := gorm.Open("mysql", "abc:123@tcp(10.10.10.10:3306)/ramos?charset=utf8&loc=Local")
    item := Ip_distribution{Ip: "1.1.1.1"} 
    fmt.Println(db.NewRecord(item))
    db.Create(&item)
    fmt.Println(db.NewRecord(item))

The 1.1.1.1 doesn't exist in mysql before run this function. But the result is :

false
false

You know, it should return true when we execute db.NewRecord(item) first time. Is this a bug or I misused this function?

@adriendomoison
Copy link

+1 I have the same problem with object having string as primary_key !

@cage1016
Copy link

cage1016 commented Jan 5, 2017

same issue +1

@jinzhu
Copy link
Member

jinzhu commented Jan 5, 2017

The NewRecord method only check if current value has primary key or not.

If you want to ensure this primary key exists in database, you need to query that like:

db.First(&Ip_distribution{}, "ip = ?", "1.1.1.1").RecordNotFound()

@SirWaithaka
Copy link

The comment on the method was confusing me too, thank you for clearing that out @jinzhu

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

5 participants