Merge b91fed614660b760d7f3a4ae3cc134b908eb7784 into 9acaa33324bbcc78239a1c913d4f1292c12177b9
This commit is contained in:
commit
8a264b988a
9
main.go
9
main.go
@ -602,6 +602,15 @@ func (s *DB) AddForeignKey(field string, dest string, onDelete string, onUpdate
|
||||
return scope.db
|
||||
}
|
||||
|
||||
// AddLongForeignKey can be used when the identifier of foreign key is exceeds 64 characters
|
||||
// It uses sha1 to shorten it
|
||||
// db.Model(&User{}).AddLongForeignKey("city_id", "cities(id)", "RESTRICT", "RESTRICT")
|
||||
func (s *DB) AddLongForeignKey(field string, dest string, onDelete string, onUpdate string) *DB {
|
||||
scope := s.clone().NewScope(s.Value)
|
||||
scope.addLongForeignKey(field, dest, onDelete, onUpdate)
|
||||
return scope.db
|
||||
}
|
||||
|
||||
// Association start `Association Mode` to handler relations things easir in that mode, refer: https://jinzhu.github.io/gorm/associations.html#association-mode
|
||||
func (s *DB) Association(column string) *Association {
|
||||
var err error
|
||||
|
19
scope.go
19
scope.go
@ -1,6 +1,7 @@
|
||||
package gorm
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
@ -1154,6 +1155,24 @@ func (scope *Scope) addIndex(unique bool, indexName string, column ...string) {
|
||||
scope.Raw(fmt.Sprintf("%s %v ON %v(%v) %v", sqlCreate, indexName, scope.QuotedTableName(), strings.Join(columns, ", "), scope.whereSQL())).Exec()
|
||||
}
|
||||
|
||||
func (scope *Scope) addLongForeignKey(field string, dest string, onDelete string, onUpdate string) {
|
||||
getHash := func(rawKey string) string {
|
||||
h := sha1.New()
|
||||
h.Write([]byte(rawKey))
|
||||
bs := h.Sum(nil)
|
||||
return fmt.Sprintf("%x", bs)
|
||||
}
|
||||
keyName := scope.TableName() + field + dest
|
||||
hash := getHash(keyName)
|
||||
query := `ALTER TABLE ` + scope.QuotedTableName() +
|
||||
` ADD CONSTRAINT ` + "`" + hash + "`" +
|
||||
` FOREIGN KEY (` + scope.quoteIfPossible(field) + `)` +
|
||||
` REFERENCES ` + dest +
|
||||
` ON DELETE ` + onDelete +
|
||||
` ON UPDATE ` + onUpdate + `;`
|
||||
scope.Raw(query).Exec()
|
||||
}
|
||||
|
||||
func (scope *Scope) addForeignKey(field string, dest string, onDelete string, onUpdate string) {
|
||||
keyName := scope.Dialect().BuildForeignKeyName(scope.TableName(), field, dest)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user