mirror of https://github.com/go-gorm/gorm.git
Use BuildKeyName to build db's index name
This commit is contained in:
parent
ca46ec0770
commit
802104cc7c
|
@ -41,8 +41,8 @@ type Dialect interface {
|
||||||
// LastInsertIdReturningSuffix most dbs support LastInsertId, but postgres needs to use `RETURNING`
|
// LastInsertIdReturningSuffix most dbs support LastInsertId, but postgres needs to use `RETURNING`
|
||||||
LastInsertIDReturningSuffix(tableName, columnName string) string
|
LastInsertIDReturningSuffix(tableName, columnName string) string
|
||||||
|
|
||||||
// BuildForeignKeyName returns a foreign key name for the given table, field and reference
|
// BuildKeyName returns a valid key name (foreign key, index key) for the given table, field and reference
|
||||||
BuildForeignKeyName(tableName, field, dest string) string
|
BuildKeyName(kind, tableName string, fields ...string) string
|
||||||
|
|
||||||
// CurrentDatabase return current database name
|
// CurrentDatabase return current database name
|
||||||
CurrentDatabase() string
|
CurrentDatabase() string
|
||||||
|
|
|
@ -144,8 +144,8 @@ func (commonDialect) LastInsertIDReturningSuffix(tableName, columnName string) s
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (DefaultForeignKeyNamer) BuildForeignKeyName(tableName, field, dest string) string {
|
func (DefaultForeignKeyNamer) BuildKeyName(kind, tableName string, fields ...string) string {
|
||||||
keyName := fmt.Sprintf("%s_%s_%s_foreign", tableName, field, dest)
|
keyName := fmt.Sprintf("%s_%s_%s", kind, tableName, strings.Join(fields, "_"))
|
||||||
keyName = regexp.MustCompile("[^a-zA-Z0-9]+").ReplaceAllString(keyName, "_")
|
keyName = regexp.MustCompile("[^a-zA-Z0-9]+").ReplaceAllString(keyName, "_")
|
||||||
return keyName
|
return keyName
|
||||||
}
|
}
|
||||||
|
|
|
@ -157,8 +157,8 @@ func (mysql) SelectFromDummyTable() string {
|
||||||
return "FROM DUAL"
|
return "FROM DUAL"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s mysql) BuildForeignKeyName(tableName, field, dest string) string {
|
func (s mysql) BuildKeyName(kind, tableName string, fields ...string) string {
|
||||||
keyName := s.commonDialect.BuildForeignKeyName(tableName, field, dest)
|
keyName := s.commonDialect.BuildKeyName(kind, tableName, fields...)
|
||||||
if utf8.RuneCountInString(keyName) <= 64 {
|
if utf8.RuneCountInString(keyName) <= 64 {
|
||||||
return keyName
|
return keyName
|
||||||
}
|
}
|
||||||
|
@ -167,7 +167,7 @@ func (s mysql) BuildForeignKeyName(tableName, field, dest string) string {
|
||||||
bs := h.Sum(nil)
|
bs := h.Sum(nil)
|
||||||
|
|
||||||
// sha1 is 40 characters, keep first 24 characters of destination
|
// sha1 is 40 characters, keep first 24 characters of destination
|
||||||
destRunes := []rune(regexp.MustCompile("[^a-zA-Z0-9]+").ReplaceAllString(dest, "_"))
|
destRunes := []rune(regexp.MustCompile("[^a-zA-Z0-9]+").ReplaceAllString(fields[0], "_"))
|
||||||
if len(destRunes) > 24 {
|
if len(destRunes) > 24 {
|
||||||
destRunes = destRunes[:24]
|
destRunes = destRunes[:24]
|
||||||
}
|
}
|
||||||
|
|
7
scope.go
7
scope.go
|
@ -1165,7 +1165,8 @@ func (scope *Scope) addIndex(unique bool, indexName string, column ...string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) addForeignKey(field string, dest string, onDelete string, onUpdate string) {
|
func (scope *Scope) addForeignKey(field string, dest string, onDelete string, onUpdate string) {
|
||||||
keyName := scope.Dialect().BuildForeignKeyName(scope.TableName(), field, dest)
|
// Compatible with old generated key
|
||||||
|
keyName := scope.Dialect().BuildKeyName(scope.TableName(), field, dest, "foreign")
|
||||||
|
|
||||||
if scope.Dialect().HasForeignKey(scope.TableName(), keyName) {
|
if scope.Dialect().HasForeignKey(scope.TableName(), keyName) {
|
||||||
return
|
return
|
||||||
|
@ -1209,7 +1210,7 @@ func (scope *Scope) autoIndex() *Scope {
|
||||||
|
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
if name == "INDEX" || name == "" {
|
if name == "INDEX" || name == "" {
|
||||||
name = fmt.Sprintf("idx_%v_%v", scope.TableName(), field.DBName)
|
name = scope.Dialect().BuildKeyName("idx", scope.TableName(), field.DBName)
|
||||||
}
|
}
|
||||||
indexes[name] = append(indexes[name], field.DBName)
|
indexes[name] = append(indexes[name], field.DBName)
|
||||||
}
|
}
|
||||||
|
@ -1220,7 +1221,7 @@ func (scope *Scope) autoIndex() *Scope {
|
||||||
|
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
if name == "UNIQUE_INDEX" || name == "" {
|
if name == "UNIQUE_INDEX" || name == "" {
|
||||||
name = fmt.Sprintf("uix_%v_%v", scope.TableName(), field.DBName)
|
name = scope.Dialect().BuildKeyName("uix", scope.TableName(), field.DBName)
|
||||||
}
|
}
|
||||||
uniqueIndexes[name] = append(uniqueIndexes[name], field.DBName)
|
uniqueIndexes[name] = append(uniqueIndexes[name], field.DBName)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue