9

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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Joff
  • 11,247
  • 16
  • 60
  • 103
  • An empty string is the "not null" version of a string, no? – Cetin Basoz Apr 24 '17 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 – Joff Apr 24 '17 at 12:11
  • 1
    @deltaskelta: That's exactly what it should do. What's your question? – Jonathan Hall Apr 24 '17 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? – Joff Apr 24 '17 at 12:20
  • 1
    No, there's no way within a struct definition to specify that some specific value is invalid. – Jonathan Hall Apr 24 '17 at 12:34
  • why don't you declare your field as sql.NullString? – Alessio Apr 24 '17 at 14:18
  • Use defaults instead `gorm:"default:'empty'"`. Or prevent calls to db.Create at application level. – Minty Apr 24 '17 at 15:12

3 Answers3

24

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.

Umar Hayat
  • 4,300
  • 1
  • 12
  • 27
  • 1
    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)` – Tomer Cagan Feb 03 '22 at 09:35
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

  • Answer is a comment on another answer – Zach Jensz Sep 23 '22 at 08:04
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32752795) – Markus Meyer Sep 25 '22 at 08:52
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.