go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
36.87k stars 3.93k forks source link

Automigration NOT NULL to DEFAULT NULL not working #5643

Closed pjarmalavicius closed 1 month ago

pjarmalavicius commented 2 years ago

GORM Playground Link

https://github.com/go-gorm/playground/pull/512

Description

It's not possible to change not nullable field to nullable.

type Test struct {
    gorm.Model
    SomeStr string `gorm:"type:VARCHAR(15);NOT NULL"`
}

After AutoMigrate is run, some_str field on DB is not nullable, then changing struct to this:

type Test struct {
    gorm.Model
    SomeStr string `gorm:"type:VARCHAR(15);default:NULL"`
}

and running AutoMigrate again, field remains not nullable. I believe problem is here: https://github.com/go-gorm/gorm/blob/master/migrator/migrator.go#L439 both nullable (actual state on DB) and field.NotNull (new value) are false, meaning it's only possible to change from nullable to not nullable. I don't know if it's done on purpose.

a631807682 commented 2 years ago

It is a bug, are you interested in creating a PR to fix it?

Joaolfc0 commented 2 months ago

This bug is still happening for me

a631807682 commented 1 month ago

https://github.com/go-gorm/gorm.io/pull/782