-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Closed
Labels
type:feature_requestfeature requestfeature requesttype:with reproduction stepswith reproduction stepswith reproduction steps
Description
GORM Playground Link
Description
When a user wants to add a unique constraint on a column, the unique constraint is not added by AutoMigrate().
When creating a new table, the constraint is added.
When adding a unique index on an existing column, the migration is applied.
The MigrateColumn function do not seems to handle unique fields :
Lines 363 to 404 in 2ba612e
| func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnType gorm.ColumnType) error { | |
| // found, smart migrate | |
| fullDataType := strings.ToLower(m.DB.Migrator().FullDataTypeOf(field).SQL) | |
| realDataType := strings.ToLower(columnType.DatabaseTypeName()) | |
| alterColumn := false | |
| // check size | |
| if length, _ := columnType.Length(); length != int64(field.Size) { | |
| if length > 0 && field.Size > 0 { | |
| alterColumn = true | |
| } else { | |
| // has size in data type and not equal | |
| matches := regexp.MustCompile(`[^\d](\d+)[^\d]?`).FindAllStringSubmatch(realDataType, -1) | |
| matches2 := regexp.MustCompile(`[^\d]*(\d+)[^\d]?`).FindAllStringSubmatch(fullDataType, -1) | |
| if (len(matches) == 1 && matches[0][1] != fmt.Sprint(field.Size) || !field.PrimaryKey) && (len(matches2) == 1 && matches2[0][1] != fmt.Sprint(length)) { | |
| alterColumn = true | |
| } | |
| } | |
| } | |
| // check precision | |
| if precision, _, ok := columnType.DecimalSize(); ok && int64(field.Precision) != precision { | |
| if regexp.MustCompile(fmt.Sprintf("[^0-9]%d[^0-9]", field.Precision)).MatchString(m.DataTypeOf(field)) { | |
| alterColumn = true | |
| } | |
| } | |
| // check nullable | |
| if nullable, ok := columnType.Nullable(); ok && nullable == field.NotNull { | |
| // not primary key & database is nullable | |
| if !field.PrimaryKey && nullable { | |
| alterColumn = true | |
| } | |
| } | |
| if alterColumn && !field.IgnoreMigration { | |
| return m.DB.Migrator().AlterColumn(value, field.Name) | |
| } | |
| return nil | |
| } |
Thanks !
shprotru, joshuachinemezu, VishwasShashidhar, shivkumarsingh7, duy-chk and 10 more
Metadata
Metadata
Assignees
Labels
type:feature_requestfeature requestfeature requesttype:with reproduction stepswith reproduction stepswith reproduction steps