45 lines
1.4 KiB
Go
45 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type TestSettings struct {
|
|
ID uint `gorm:"primaryKey"`
|
|
AccountID uint `gorm:"not null;uniqueIndex"`
|
|
Name string `gorm:"size:50"`
|
|
AutoProvision bool `gorm:"default:true"`
|
|
Active bool `gorm:"default:true"`
|
|
}
|
|
|
|
func main() {
|
|
db, err := gorm.Open(sqlite.Open("test_bool.db"), &gorm.Config{})
|
|
if err != nil {
|
|
fmt.Println("ERROR:", err)
|
|
return
|
|
}
|
|
db.AutoMigrate(&TestSettings{})
|
|
|
|
// Approach: Create with AutoProvision=true (non-zero), then update to false
|
|
s := TestSettings{AccountID: 1, Name: "test1", AutoProvision: true, Active: true}
|
|
db.Create(&s)
|
|
fmt.Printf("After Create: ID=%d\n", s.ID)
|
|
|
|
// Now update AutoProvision to false
|
|
db.Model(&TestSettings{}).Where("id = ?", s.ID).Update("auto_provision", false)
|
|
|
|
var r TestSettings
|
|
db.First(&r, s.ID)
|
|
fmt.Printf("Final: AutoProvision=%v, Active=%v, ID=%d\n", r.AutoProvision, r.Active, r.ID)
|
|
|
|
// Also test: what happens if we try Create with Active=false?
|
|
s2 := TestSettings{AccountID: 2, Name: "test2", AutoProvision: true, Active: true}
|
|
db.Create(&s2)
|
|
db.Model(&TestSettings{}).Where("id = ?", s2.ID).Updates(map[string]interface{}{"auto_provision": false, "active": false})
|
|
|
|
var r2 TestSettings
|
|
db.First(&r2, s2.ID)
|
|
fmt.Printf("Final2: AutoProvision=%v, Active=%v\n", r2.AutoProvision, r2.Active)
|
|
} |