-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Description
Your Question
Why does the default value on a column being the same between the database and the struct prevent an alter being generated for a column? (For SQLite it's a drop/recreate that should be done)
For example a struct with:
TextColumn string `gorm:"type:bytes;size:255;index;default:'';"
Against an SQLite database with
text_column TEXT NOT NULL COLLATE BINARY DEFAULT ''
Will not generate an alter statement because the default: statement below results in false.
Note the difference between Bool and default vs Time and String.
Lines 554 to 569 in 0011f39
| switch field.GORMDataType { | |
| case schema.Time: | |
| if !strings.EqualFold(strings.TrimSuffix(dv, "()"), strings.TrimSuffix(field.DefaultValue, "()")) { | |
| alterColumn = true | |
| } | |
| case schema.Bool: | |
| v1, _ := strconv.ParseBool(dv) | |
| v2, _ := strconv.ParseBool(field.DefaultValue) | |
| alterColumn = v1 != v2 | |
| case schema.String: | |
| if dv != field.DefaultValue && dv != strings.Trim(field.DefaultValue, "'\"") { | |
| alterColumn = true | |
| } | |
| default: | |
| alterColumn = dv != field.DefaultValue | |
| } |
Bool and default allow the DEFAULT being the same to change a column from being altered to not being altered.
Time and String will mark a column for altering, but NOT remove a column from being altered.
The document you expected this should be explained
https://gorm.io/docs/migration.html#Auto-Migration
Expected answer
If this is working as designed (ie. the DEFAULT on a column is the decider on whether it is altered), then it should be documented as so.