GORM Not ignoring field with `gorm:"-"`

Datsik picture Datsik · May 1, 2016 · Viewed 11.9k times · Source

Using Jinzhu's GORM Package which is fantastic btw, I currently have this struct:

type User struct {
    gorm.Model

    // The Users username
    Username string `gorm:"size:255;unique;not null"`

    // The Users email address
    Email string `gorm:"size:255;unique;not null"`

    // The Users hashed password
    Password string `gorm:"size:255;not null"`

    // The Users password confirmation (only for forms)
    PasswordC string `gorm:"-"`

    // The Users FULL NAME (e.g. Burt Reynolds)
    Fullname string `gorm:"size:255; not null"`

    // The Users Karma level
    Karma int

    // Is the user banned?
    Banned bool
}

But I also use Gorilla's Schema package so any form values populate the struct, but I don't want the PasswordC to be saved into the database because it will be plain text as the normal Password field gets bcrypt'd so any information on how to make GORM not save the PasswordC field.

Answer

bobisme picture bobisme · May 2, 2016

The docs say gorm:"-", but the code indicates sql:"-" is the correct syntax.

My testing verifies this.