Fix too long foreign key/checker names, close #4026

This commit is contained in:
Jinzhu 2021-02-01 10:37:12 +08:00
parent 7598204dc3
commit db0cc4d60b
1 changed files with 11 additions and 8 deletions

View File

@ -54,27 +54,30 @@ func (ns NamingStrategy) JoinTableName(str string) string {
// RelationshipFKName generate fk name for relation
func (ns NamingStrategy) RelationshipFKName(rel Relationship) string {
return strings.Replace(fmt.Sprintf("fk_%s_%s", rel.Schema.Table, ns.toDBName(rel.Name)), ".", "_", -1)
return ns.formatName("fk", rel.Schema.Table, ns.toDBName(rel.Name))
}
// CheckerName generate checker name
func (ns NamingStrategy) CheckerName(table, column string) string {
return strings.Replace(fmt.Sprintf("chk_%s_%s", table, column), ".", "_", -1)
return ns.formatName("chk", table, column)
}
// IndexName generate index name
func (ns NamingStrategy) IndexName(table, column string) string {
idxName := fmt.Sprintf("idx_%v_%v", table, ns.toDBName(column))
idxName = strings.Replace(idxName, ".", "_", -1)
return ns.formatName("idx", table, ns.toDBName(column))
}
if utf8.RuneCountInString(idxName) > 64 {
func (ns NamingStrategy) formatName(prefix, table, name string) string {
formatedName := strings.Replace(fmt.Sprintf("%v_%v_%v", prefix, table, name), ".", "_", -1)
if utf8.RuneCountInString(formatedName) > 64 {
h := sha1.New()
h.Write([]byte(idxName))
h.Write([]byte(formatedName))
bs := h.Sum(nil)
idxName = fmt.Sprintf("idx%v%v", table, column)[0:56] + string(bs)[:8]
formatedName = fmt.Sprintf("%v%v%v", prefix, table, name)[0:56] + string(bs)[:8]
}
return idxName
return formatedName
}
var (