forked from mirror/gorm
AddUniqueIndex
This commit is contained in:
parent
cdce84fd1a
commit
d1dc0ccbef
|
@ -930,7 +930,13 @@ db.Table("users").Select("users.name, emails.email").Joins("left join emails on
|
||||||
db.Model(User{}).AddIndex("idx_user_name", "name")
|
db.Model(User{}).AddIndex("idx_user_name", "name")
|
||||||
|
|
||||||
// multiple column index
|
// multiple column index
|
||||||
db.Model(User{}).AddIndex("idx_user_name", "name", "age")
|
db.Model(User{}).AddIndex("idx_user_name_age", "name", "age")
|
||||||
|
|
||||||
|
// single column unique index
|
||||||
|
db.Model(User{}).AddUniqueIndex("idx_user_name", "name")
|
||||||
|
|
||||||
|
// multiple column unique index
|
||||||
|
db.Model(User{}).AddUniqueIndex("idx_user_name_age", "name", "age")
|
||||||
```
|
```
|
||||||
|
|
||||||
## Run Raw SQL
|
## Run Raw SQL
|
||||||
|
|
7
main.go
7
main.go
|
@ -315,7 +315,12 @@ func (s *DB) DropColumn(column string) *DB {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) AddIndex(indexName string, column ...string) *DB {
|
func (s *DB) AddIndex(indexName string, column ...string) *DB {
|
||||||
s.clone().NewScope(s.Value).addIndex(indexName, column...)
|
s.clone().NewScope(s.Value).addIndex(false, indexName, column...)
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *DB) AddUniqueIndex(indexName string, column ...string) *DB {
|
||||||
|
s.clone().NewScope(s.Value).addIndex(true, indexName, column...)
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2026,7 +2026,15 @@ func TestIndices(t *testing.T) {
|
||||||
if err := db.Model(&UserCompany{}).RemoveIndex("idx_user_company_user").Error; err != nil {
|
if err := db.Model(&UserCompany{}).RemoveIndex("idx_user_company_user").Error; err != nil {
|
||||||
t.Errorf("Got error when tried to remove index: %+v", err)
|
t.Errorf("Got error when tried to remove index: %+v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := db.Model(&UserCompany{}).AddIndex("idx_user_company_user_company", "user_id", "company_id").Error; err != nil {
|
if err := db.Model(&UserCompany{}).AddIndex("idx_user_company_user_company", "user_id", "company_id").Error; err != nil {
|
||||||
t.Errorf("Got error when tried to create index: %+v", err)
|
t.Errorf("Got error when tried to create index: %+v", err)
|
||||||
}
|
}
|
||||||
|
if err := db.Model(&UserCompany{}).RemoveIndex("idx_user_company_user_company").Error; err != nil {
|
||||||
|
t.Errorf("Got error when tried to remove index: %+v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.Model(&UserCompany{}).AddUniqueIndex("idx_user_company_user_company", "user_id", "company_id").Error; err != nil {
|
||||||
|
t.Errorf("Got error when tried to create index: %+v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -431,13 +431,18 @@ func (scope *Scope) dropColumn(column string) {
|
||||||
scope.Raw(fmt.Sprintf("ALTER TABLE %v DROP COLUMN %v", scope.Quote(scope.TableName()), scope.Quote(column))).Exec()
|
scope.Raw(fmt.Sprintf("ALTER TABLE %v DROP COLUMN %v", scope.Quote(scope.TableName()), scope.Quote(column))).Exec()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) addIndex(indexName string, column ...string) {
|
func (scope *Scope) addIndex(unique bool, indexName string, column ...string) {
|
||||||
var columns []string
|
var columns []string
|
||||||
for _, name := range column {
|
for _, name := range column {
|
||||||
columns = append(columns, scope.Quote(name))
|
columns = append(columns, scope.Quote(name))
|
||||||
}
|
}
|
||||||
|
|
||||||
scope.Raw(fmt.Sprintf("CREATE INDEX %v ON %v(%v);", indexName, scope.Quote(scope.TableName()), strings.Join(columns, ", "))).Exec()
|
sqlCreate := "CREATE INDEX"
|
||||||
|
if unique {
|
||||||
|
sqlCreate = "CREATE UNIQUE INDEX"
|
||||||
|
}
|
||||||
|
|
||||||
|
scope.Raw(fmt.Sprintf("%s %v ON %v(%v);", sqlCreate, indexName, scope.Quote(scope.TableName()), strings.Join(columns, ", "))).Exec()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) removeIndex(indexName string) {
|
func (scope *Scope) removeIndex(indexName string) {
|
||||||
|
|
Loading…
Reference in New Issue