Preventing null (or empty string values in the db)
Asked Answered
M

4

13

I'm using PostgreSQL and GORM in my Go app.

I thought that using the sql tab of sql:"not null" would do the trick of preventing a null entry, but when go initializes structs with a string type then it defaults to an empty string which is not the same as null in the db.

I am wondering if there is a way to prevent this from happening in a struct definition so I wouldn't have to strictly enforce it at all levels in the application code.

Mcmullan answered 24/4, 2017 at 12:4 Comment(7)
An empty string is the "not null" version of a string, no?Colombia
its not enforcing it for me..If I got to my db manually and try to enter a null field then it says it violates not null constraint, but if I enter '' then it accepts itMcmullan
@deltaskelta: That's exactly what it should do. What's your question?Rejoin
the last line of my question is what I am asking. Is there a way to prevent an empty string from being valid without strictly enforcing it through application code?Mcmullan
No, there's no way within a struct definition to specify that some specific value is invalid.Rejoin
why don't you declare your field as sql.NullString?Upgrade
Use defaults instead gorm:"default:'empty'". Or prevent calls to db.Create at application level.Atheroma
S
30

You can solve this problem of preventing '' (empty) string insertion into the database by using default: null and not null constraint together. SO if Struct field values are empty it will be consider as default value null and gorm will throw an error.

gorm:"unique;not null;type:varchar(100);default:null"

Example is:

type User struct {
    gorm.Model
    Email  string    `gorm:"unique;not null;type:varchar(100);default:null"`

}

SO gorm for empty User.Email will throw an error.

Straw answered 18/12, 2019 at 12:18 Comment(2)
This works nicely. Nice way to solve this! Setting it this way, trying to pass in empty Email would create an error similar to: ERROR: null value in column "email" of relation "users" violates not-null constraint (SQLSTATE 23502)Exorbitance
This works for sqllite but mysql throws an error that Error 1067 (42000): Invalid default value forSubbasement
S
0
type User struct {
    gorm.Model
    Email  string    `gorm:"unique;not null;type:varchar(100);default:null"`

}

When I use like this it works but when I automigrate I did had error from migration gorm

Subsequence answered 19/9, 2022 at 11:36 Comment(2)
Answer is a comment on another answerPoland
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewBraque
A
0

Add a isValid() function to your model that returns false if a not null field is in its defalt (0 for int, "" for string, etc), and before insert, check if valid, return an error if not valid, else, nil.

Assiduous answered 4/1, 2023 at 22:35 Comment(0)
G
0

A reasonable approach is using a BeforeCreate hook to validate the Email field and return an error if it is empty string. This way, you can ensure that object is only created when the required fields are properly populated.

// BeforeCreate hook to validate email field
func (u *User) BeforeCreate(tx *gorm.DB) (err error) {
    if u.Email == ""  {
        return errors.New("email cannot be empty")
    }
    return nil
}
Gotland answered 29/4 at 12:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.