mirror of https://github.com/go-gorm/gorm.git
Fix too long foreign key/checker names, close #4026
This commit is contained in:
parent
7598204dc3
commit
db0cc4d60b
|
@ -54,27 +54,30 @@ func (ns NamingStrategy) JoinTableName(str string) string {
|
||||||
|
|
||||||
// RelationshipFKName generate fk name for relation
|
// RelationshipFKName generate fk name for relation
|
||||||
func (ns NamingStrategy) RelationshipFKName(rel Relationship) string {
|
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
|
// CheckerName generate checker name
|
||||||
func (ns NamingStrategy) CheckerName(table, column string) string {
|
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
|
// IndexName generate index name
|
||||||
func (ns NamingStrategy) IndexName(table, column string) string {
|
func (ns NamingStrategy) IndexName(table, column string) string {
|
||||||
idxName := fmt.Sprintf("idx_%v_%v", table, ns.toDBName(column))
|
return ns.formatName("idx", table, ns.toDBName(column))
|
||||||
idxName = strings.Replace(idxName, ".", "_", -1)
|
}
|
||||||
|
|
||||||
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 := sha1.New()
|
||||||
h.Write([]byte(idxName))
|
h.Write([]byte(formatedName))
|
||||||
bs := h.Sum(nil)
|
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 (
|
var (
|
||||||
|
|
Loading…
Reference in New Issue