wuhua
wuhua

Reputation: 13

go-GORM cannot insert value

Sorry for my poor English.

I use db.Create() to insert value in my code.

Here is my model

type users struct {
    id       int        `json:"id"`
    name     string     `json:"name"`
    email    string     `json:"email"`
    password string     `json:"password" `
    website  string     `json:"website" `
    nickname string     `json:"nickname"`
    grade    string     `json:"grade"`
    bracket  string     `json:"bracket"`
    banned   bool       `json:"banned"`
    verified bool       `json:"verified"`
    admin    bool       `json:"admin"`
    joined   *time.Time `json:"joined"`
}

and code

    db.Create(&users{
        name:     name,
        email:    strings.ToLower(email),
        password: password,
        grade:    number,
    })

After execution, the sql was wrong.

[2020-02-11 14:29:07]  [0.32ms]  INSERT INTO `users` VALUES()  

The other operation is good, such as First(),Where().

I tried to use NewRecord() or Save() instead, but the result was the same.

Please tell me where my code went wrong.

Upvotes: 1

Views: 1055

Answers (1)

Ramanujan R
Ramanujan R

Reputation: 1719

It seems you are using gorm as ORM framework. If so, the model properties must be exported. You have to define the struct like this.

type users struct {
    Id       int        `json:"id"`
    Name     string     `json:"name"`
    Email    string     `json:"email"`
    Password string     `json:"password" `
    Website  string     `json:"website" `
    Nickname string     `json:"nickname"`
    Grade    string     `json:"grade"`
    Bracket  string     `json:"bracket"`
    Banned   bool       `json:"banned"`
    Verified bool       `json:"verified"`
    Admin    bool       `json:"admin"`
    Joined   *time.Time `json:"joined"`
}

Upvotes: 1

Related Questions