When a struct has gorm default set to a non-zero value, if the value being created is a zero value, it will be ignored and the default will be used.
type Source struct {
gorm.Model
Name string
URL string `gorm:"unique;unique_index"`
Cache bool `gorm:"default:true"
}
source := Source{
Name: "Test",
URL: "https://test.com",
Cache: false,
}
db.Create(&source)
The output:
[5.831ms] [rows:1] INSERT INTO `sources` (`created_at`,`updated_at`,`deleted_at`,`name`,`url`,`cache`) VALUES ("2022-08-04 14:26:16.065","2022-08-04 14:26:16.065",NULL,"Test","https://test.com",true) RETURNING `id`
The expected behavior should be:
[5.831ms] [rows:1] INSERT INTO `sources` (`created_at`,`updated_at`,`deleted_at`,`name`,`url`,`cache`) VALUES ("2022-08-04 14:26:16.065","2022-08-04 14:26:16.065",NULL,"Test","https://test.com",false) RETURNING `id`
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: jlagermann
After some additional testing, I changed the description. When a struct has gorm default set to a non-zero value, if the value being created is a zero value, it will be ignored and the default will be used.
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: a631807682
duplicated of https://github.com/go-gorm/gorm/issues/5541
Comment From: jlagermann
This is similar to #5541 but not a duplicate. #5541 only addresses bool values. The issue is related to creating a record with a zero value that has a non-zero default.
Comment From: a631807682
If a value is zero, there is no way to tell whether it is a default value or a user-specified value. You can also use *bool to tell us it is specified value
The reason here also applies to other zero-value types
Comment From: jlagermann
Is there a way to force it to create with the zero value? I can update with zero values with db.Select("*").Updates(&source). Is there a similar method for Create()?
Comment From: a631807682
Not currently supported, are you interested in creating a PR for this?
Comment From: jlagermann
I would be interested if I understood all the pieces that need to be updated to support this. I'm using sqlite3.
Without this capability, the gorm defaults don't really work. The idea with defaults is to provide a value when the input does not provide one. In the current implementation, the default value is used unless the input provides another non-zero value.
If you set the default for a bool to true, it will be created as true 100% of the time.
If you set a string default to some value, it can never be created with a zero value.