Fixed bug/gorm indexes
Some checks are pending
CI / Build (push) Waiting to run
CI / Test (1.23) (push) Waiting to run
CI / Test (1.24) (push) Waiting to run
CI / Test (1.25) (push) Waiting to run
CI / Lint (push) Waiting to run

This commit is contained in:
2025-12-18 19:15:22 +02:00
parent b7950057eb
commit d93a4b6f08
4 changed files with 622 additions and 142 deletions

View File

@@ -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
}
}
}