model只有一个字段并设置为unique,使用AutoMigrate,第二次启动服务Migrate会报错

type Labels struct {
    Label string `gorm:"column:label;size:50;unique;not null;"`
}

...
if err = DB.AutoMigrate(Labels{}); err != nil {
       panic(err)
}
2024/08/22 16:51:33  Error 1061 (42000): Duplicate key name 'uni_labels_label'
[22.362ms] [rows:0] ALTER TABLE `labels` ADD CONSTRAINT `uni_labels_label` UNIQUE (`label`)
panic: Error 1061 (42000): Duplicate key name 'uni_labels_label'

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: 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: 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: xxxVitoxxx

In your example, the column label is not formally defined as a primary key, but the combination of UNIQUE and NOT NULL constraints leads to similar enforcement of uniqueness and non-nullability, effectively treating it as a primary key in practice.

In fact, the unique key has been created, but the primary key has not.

+--------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table  | Non_unique | Key_name         | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+--------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| labels |          0 | uni_labels_label |            1 | label       | A         |           0 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
+--------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+

Although gorm has created a unique key, the label field is regarded as the primary key. As a result, in gorm, thePrimaryKeyValue of the label field's ColumnType is true, and its UniqueValue is false.

func main() {
    ct, _ := DB.Migrator().ColumnTypes(&Labels{})
    fmt.Printf("%+v\n", ct)
}
// output
// [{
//     SQLColumnType:0x14000138690 
//     NameValue:{String:label Valid:true} 
//     DataTypeValue:{String:varchar Valid:true} 
//     ColumnTypeValue:{String:varchar(50) Valid:true} 
//     PrimaryKeyValue:{Bool:true Valid:true} 
//     UniqueValue:{Bool:false Valid:true} 
//     AutoIncrementValue:{Bool:false Valid:false} 
//     LengthValue:{Int64:50 Valid:true} 
//     DecimalSizeValue:{Int64:0 Valid:false} 
//     ScaleValue:{Int64:0 Valid:false} 
//     NullableValue:{Bool:false Valid:true} 
//     ScanTypeValue:<nil> 
//     CommentValue:{String: Valid:true} 
//     DefaultValueValue:{String: Valid:false}
// }]

If you execute AutoMigrate again, a Duplicate key name error will occur because gorm determines that the unique key has not been created based on the ColumnType.

mysql/migrator.go Line 107 in v1.5.4

unique, ok := columnType.Unique()
// --snip--
if unique {
    // --snip--
} else {
    if field.Unique {
        if err := execTx.Migrator().CreateConstraint(value, constraint); err != nil {
        return err
    }
    // --snip--
}

should we avoid this error in this case?
@jinzhu

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: 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