MCPcopy
hub / github.com/go-gorm/gorm / MigrateColumnUnique

Method MigrateColumnUnique

migrator/migrator.go:599–617  ·  view source on GitHub ↗
(value interface{}, field *schema.Field, columnType gorm.ColumnType)

Source from the content-addressed store, hash-verified

597}
598
599func (m Migrator) MigrateColumnUnique(value interface{}, field *schema.Field, columnType gorm.ColumnType) error {
600 unique, ok := columnType.Unique()
601 if !ok || field.PrimaryKey {
602 return nil // skip primary key
603 }
604 // By default, ColumnType's Unique is not affected by UniqueIndex, so we don't care about UniqueIndex.
605 return m.RunWithValue(value, func(stmt *gorm.Statement) error {
606 // We're currently only receiving boolean values on `Unique` tag,
607 // so the UniqueConstraint name is fixed
608 constraint := m.DB.NamingStrategy.UniqueName(stmt.Table, field.DBName)
609 if unique && !field.Unique {
610 return m.DB.Migrator().DropConstraint(value, constraint)
611 }
612 if !unique && field.Unique {
613 return m.DB.Migrator().CreateConstraint(value, constraint)
614 }
615 return nil
616 })
617}
618
619// ColumnTypes return columnTypes []gorm.ColumnType and execErr error
620func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error) {

Callers

nothing calls this directly

Implementers 1

Migratormigrator/migrator.go

Calls 6

RunWithValueMethod · 0.95
UniqueMethod · 0.65
UniqueNameMethod · 0.65
DropConstraintMethod · 0.65
MigratorMethod · 0.65
CreateConstraintMethod · 0.65

Tested by

no test coverage detected