Regard to #5538, clarify testing below:

func TestIssue5538(t *testing.T) {
    type Policy struct {
        gorm.Model
        Name      string `gorm:"unique;unique_index"`
        BoolFalse bool   `gorm:"default:false"`
        BoolTrue  bool   `gorm:"default:true"`
    }

    // Open the Database
    db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{
        Logger: logger.Default.LogMode(logger.Info),
        NamingStrategy: schema.NamingStrategy{
            SingularTable: true,
        },
    })
    assert.Nil(t, err)

    // Create the tables
    err = db.AutoMigrate(&Policy{})
    assert.Nil(t, err)

    // Create a new policy with all default settings
    policy1 := Policy{Name: "policy10"}
    result1 := db.Create(&policy1)
    assert.Nil(t, result1.Error)
    assert.Equal(t, uint(1), policy1.ID)
    assert.False(t, policy1.BoolFalse)
    assert.True(t, policy1.BoolTrue)

    policy2 := Policy{Name: "policy20", BoolFalse: true, BoolTrue: false}
    result2 := db.Create(&policy2)
    assert.Nil(t, result2.Error)
    assert.Equal(t, uint(2), policy2.ID)
    assert.True(t, policy2.BoolFalse)
    assert.False(t, policy2.BoolTrue) // NOT RIGHT HERE

    // Save
    policy3 := Policy{Name: "policy30"}
    result3 := db.Save(&policy3)
    assert.Nil(t, result3.Error)
    assert.Equal(t, uint(3), policy3.ID)
    assert.False(t, policy3.BoolFalse)
    assert.True(t, policy3.BoolTrue)

    policy4 := Policy{Name: "policy40", BoolFalse: true, BoolTrue: false}
    result4 := db.Save(&policy4)
    assert.Nil(t, result4.Error)
    assert.Equal(t, uint(4), policy4.ID)
    assert.True(t, policy4.BoolFalse)
    assert.False(t, policy4.BoolTrue) // NOT RIGHT HERE
}

policy2.BoolTrue and policy4.BoolTrue is not right. Is this a bug?

Add playground PR: https://github.com/go-gorm/playground/pull/617

Thanks

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

There is no way to know the difference between the bool type is not set and set to false, you can use *bool or sql.NullBool or other custom types.

https://gorm.io/docs/create.html#Default-Values

Comment From: pedia

NOTE You have to setup the default tag for fields having default or virtual/generated value in database, if you want to skip a default value definition when migrating, you could use default:(-)...

It worked. Thanks