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.

7
  • An empty string is the "not null" version of a string, no? Commented Apr 24, 2017 at 12:09
  • 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 it Commented Apr 24, 2017 at 12:11
  • 1
    @deltaskelta: That's exactly what it should do. What's your question? Commented Apr 24, 2017 at 12:19
  • 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? Commented Apr 24, 2017 at 12:20
  • 1
    No, there's no way within a struct definition to specify that some specific value is invalid. Commented Apr 24, 2017 at 12:34

4 Answers 4

33

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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)
This works for sqllite but mysql throws an error that Error 1067 (42000): Invalid default value for
1

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
}

Comments

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

2 Comments

Answer is a comment on another answer
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 Review
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.