Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

go

go-gorm

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.

like image 630
Datsik Avatar asked May 01 '16 04:05

Datsik


People also ask

How do you ignore a field in Gorm?

The gorm docs indicate that's the correct syntax for ignoring a field: jinzhu.me/gorm/models.html#model-definition - are you seeing otherwise?

Is Gorm case sensitive?

Tags are optional to use when declaring models, GORM supports the following tags: Tags are case insensitive, however camelCase is preferred.

What is Gorm DB?

DB. GORM provides the method DB which returns a generic database interface *sql.DB from the current *gorm.DB. // Get generic database object sql.DB to use its functions. sqlDB, err := db.DB()


1 Answers

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

My testing verifies this.

like image 74
bobisme Avatar answered Sep 20 '22 19:09

bobisme