From 2ff44ee8d72785386e42e11f637ac8fa816cc4ca Mon Sep 17 00:00:00 2001 From: s-takehana Date: Wed, 31 Jan 2018 17:32:36 +0900 Subject: [PATCH] Fix regex in BuildForeignKeyName #1681 (#1728) --- dialect_common.go | 2 +- dialect_mysql.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dialect_common.go b/dialect_common.go index a99627f2..7d0c3ce7 100644 --- a/dialect_common.go +++ b/dialect_common.go @@ -146,7 +146,7 @@ func (commonDialect) LastInsertIDReturningSuffix(tableName, columnName string) s func (DefaultForeignKeyNamer) BuildForeignKeyName(tableName, field, dest string) string { keyName := fmt.Sprintf("%s_%s_%s_foreign", tableName, field, dest) - keyName = regexp.MustCompile("(_*[^a-zA-Z]+_*|_+)").ReplaceAllString(keyName, "_") + keyName = regexp.MustCompile("[^a-zA-Z0-9]+").ReplaceAllString(keyName, "_") return keyName } diff --git a/dialect_mysql.go b/dialect_mysql.go index 6fcd0079..686ad1ee 100644 --- a/dialect_mysql.go +++ b/dialect_mysql.go @@ -166,8 +166,8 @@ func (s mysql) BuildForeignKeyName(tableName, field, dest string) string { h.Write([]byte(keyName)) bs := h.Sum(nil) - // sha1 is 40 digits, keep first 24 characters of destination - destRunes := []rune(regexp.MustCompile("(_*[^a-zA-Z]+_*|_+)").ReplaceAllString(dest, "_")) + // sha1 is 40 characters, keep first 24 characters of destination + destRunes := []rune(regexp.MustCompile("[^a-zA-Z0-9]+").ReplaceAllString(dest, "_")) if len(destRunes) > 24 { destRunes = destRunes[:24] }