Fixed bug/gorm indexes
This commit is contained in:
@@ -196,15 +196,31 @@ func (tm *TypeMapper) BuildBunTag(column *models.Column, table *models.Table) st
|
||||
parts = append(parts, "nullzero")
|
||||
}
|
||||
|
||||
// Check for unique constraint
|
||||
// Check for indexes (unique indexes should be added to tag)
|
||||
if table != nil {
|
||||
for _, constraint := range table.Constraints {
|
||||
if constraint.Type == models.UniqueConstraint {
|
||||
for _, col := range constraint.Columns {
|
||||
if col == column.Name {
|
||||
parts = append(parts, "unique")
|
||||
break
|
||||
for _, index := range table.Indexes {
|
||||
if !index.Unique {
|
||||
continue
|
||||
}
|
||||
// Check if this column is in the index
|
||||
for _, col := range index.Columns {
|
||||
if col == column.Name {
|
||||
// Add unique tag with index name for composite indexes
|
||||
// or simple unique for single-column indexes
|
||||
if len(index.Columns) > 1 {
|
||||
// Composite index - use index name
|
||||
parts = append(parts, fmt.Sprintf("unique:%s", index.Name))
|
||||
} else {
|
||||
// Single column - use index name if it's not auto-generated
|
||||
// Auto-generated names typically follow pattern: idx_tablename_columnname
|
||||
expectedAutoName := fmt.Sprintf("idx_%s_%s", table.Name, column.Name)
|
||||
if index.Name == expectedAutoName {
|
||||
parts = append(parts, "unique")
|
||||
} else {
|
||||
parts = append(parts, fmt.Sprintf("unique:%s", index.Name))
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user