-
-
Save junhuif/59c06b300de81d2c7f86 to your computer and use it in GitHub Desktop.
Gorm don't insert false value to database for boolean field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/jinzhu/gorm" | |
_ "github.com/lib/pq" | |
_ "github.com/mattn/go-sqlite3" | |
) | |
var DB *gorm.DB | |
type Post struct { | |
Title string | |
CommentsEnabled bool `sql:"default:true"` | |
} | |
func panicIfErr(err error) { | |
if err != nil { | |
panic(err) | |
} | |
} | |
func init() { | |
db, err := gorm.Open("postgres", "user=hui dbname=gorm_test sslmode=disable") | |
panicIfErr(err) | |
DB = &db | |
DB.AutoMigrate(&Post{}) | |
} | |
func main() { | |
post := Post{Title: "A test post", CommentsEnabled: false} | |
err := DB.Debug().Create(&post).Error | |
panicIfErr(err) | |
fmt.Println(post.CommentsEnabled) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment