mirror of https://github.com/go-gorm/gorm.git
Rename joinTable to relationship
This commit is contained in:
parent
e1e68debdc
commit
2356182ea7
|
@ -85,11 +85,11 @@ func (association *Association) Delete(values ...interface{}) *Association {
|
|||
if len(primaryKeys) == 0 {
|
||||
association.err(errors.New("no primary key found"))
|
||||
} else {
|
||||
joinTable := association.Field.JoinTable
|
||||
relationship := association.Field.Relationship
|
||||
// many to many
|
||||
if joinTable.joinTable != "" {
|
||||
whereSql := fmt.Sprintf("%v.%v IN (?)", joinTable.joinTable, scope.Quote(ToSnake(joinTable.associationForeignKey)))
|
||||
scope.db.Table(joinTable.joinTable).Where(whereSql, primaryKeys).Delete("")
|
||||
if relationship.joinTable != "" {
|
||||
whereSql := fmt.Sprintf("%v.%v IN (?)", relationship.joinTable, scope.Quote(ToSnake(relationship.associationForeignKey)))
|
||||
scope.db.Table(relationship.joinTable).Where(whereSql, primaryKeys).Delete("")
|
||||
} else {
|
||||
association.err(errors.New("only many to many support delete"))
|
||||
}
|
||||
|
@ -106,22 +106,22 @@ func (association *Association) Clear(value interface{}) *Association {
|
|||
}
|
||||
|
||||
func (association *Association) Count() (count int) {
|
||||
joinTable := association.Field.JoinTable
|
||||
relationship := association.Field.Relationship
|
||||
scope := association.Scope
|
||||
field := scope.IndirectValue().FieldByName(association.Column)
|
||||
fieldValue := field.Interface()
|
||||
|
||||
// many to many
|
||||
if joinTable.joinTable != "" {
|
||||
if relationship.joinTable != "" {
|
||||
newScope := scope.New(fieldValue)
|
||||
whereSql := fmt.Sprintf("%v.%v IN (SELECT %v.%v FROM %v WHERE %v.%v = ?)",
|
||||
newScope.QuotedTableName(),
|
||||
scope.Quote(newScope.PrimaryKey()),
|
||||
joinTable.joinTable,
|
||||
scope.Quote(joinTable.associationForeignKey),
|
||||
joinTable.joinTable,
|
||||
joinTable.joinTable,
|
||||
scope.Quote(joinTable.foreignKey))
|
||||
relationship.joinTable,
|
||||
scope.Quote(relationship.associationForeignKey),
|
||||
relationship.joinTable,
|
||||
relationship.joinTable,
|
||||
scope.Quote(relationship.foreignKey))
|
||||
scope.db.Table(newScope.QuotedTableName()).Where(whereSql, scope.PrimaryKey()).Count(&count)
|
||||
}
|
||||
// association.Scope.related(value, association.Column)
|
||||
|
|
|
@ -32,8 +32,8 @@ func SaveBeforeAssociations(scope *Scope) {
|
|||
scope.SetColumn(field.Name, value.Interface())
|
||||
}
|
||||
|
||||
if field.JoinTable != nil && field.JoinTable.foreignKey != "" {
|
||||
scope.SetColumn(field.JoinTable.foreignKey, newDB.NewScope(value.Interface()).PrimaryKeyValue())
|
||||
if field.Relationship != nil && field.Relationship.foreignKey != "" {
|
||||
scope.SetColumn(field.Relationship.foreignKey, newDB.NewScope(value.Interface()).PrimaryKeyValue())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -50,18 +50,18 @@ func SaveAfterAssociations(scope *Scope) {
|
|||
newDB := scope.NewDB()
|
||||
elem := value.Index(i).Addr().Interface()
|
||||
|
||||
if field.JoinTable != nil && field.JoinTable.joinTable == "" && field.JoinTable.foreignKey != "" {
|
||||
newDB.NewScope(elem).SetColumn(field.JoinTable.foreignKey, scope.PrimaryKeyValue())
|
||||
if field.Relationship != nil && field.Relationship.joinTable == "" && field.Relationship.foreignKey != "" {
|
||||
newDB.NewScope(elem).SetColumn(field.Relationship.foreignKey, scope.PrimaryKeyValue())
|
||||
}
|
||||
|
||||
scope.Err(newDB.Save(elem).Error)
|
||||
|
||||
if field.JoinTable != nil && field.JoinTable.joinTable != "" {
|
||||
if field.Relationship != nil && field.Relationship.joinTable != "" {
|
||||
newScope := scope.New(elem)
|
||||
joinTable := field.JoinTable.joinTable
|
||||
foreignKey := ToSnake(field.JoinTable.foreignKey)
|
||||
joinTable := field.Relationship.joinTable
|
||||
foreignKey := ToSnake(field.Relationship.foreignKey)
|
||||
foreignValue := fmt.Sprintf("%v", scope.PrimaryKeyValue())
|
||||
associationForeignKey := ToSnake(field.JoinTable.associationForeignKey)
|
||||
associationForeignKey := ToSnake(field.Relationship.associationForeignKey)
|
||||
associationForeignValue := fmt.Sprintf("%v", newScope.PrimaryKeyValue())
|
||||
|
||||
newScope.Raw(fmt.Sprintf(
|
||||
|
@ -84,8 +84,8 @@ func SaveAfterAssociations(scope *Scope) {
|
|||
default:
|
||||
newDB := scope.NewDB()
|
||||
if value.CanAddr() {
|
||||
if field.JoinTable != nil {
|
||||
newDB.NewScope(field.Value).SetColumn(field.JoinTable.foreignKey, scope.PrimaryKeyValue())
|
||||
if field.Relationship != nil {
|
||||
newDB.NewScope(field.Value).SetColumn(field.Relationship.foreignKey, scope.PrimaryKeyValue())
|
||||
}
|
||||
scope.Err(newDB.Save(field.Value).Error)
|
||||
} else {
|
||||
|
@ -96,8 +96,8 @@ func SaveAfterAssociations(scope *Scope) {
|
|||
}
|
||||
|
||||
elem := destValue.Addr().Interface()
|
||||
if field.JoinTable != nil {
|
||||
newDB.NewScope(elem).SetColumn(field.JoinTable.foreignKey, scope.PrimaryKeyValue())
|
||||
if field.Relationship != nil {
|
||||
newDB.NewScope(elem).SetColumn(field.Relationship.foreignKey, scope.PrimaryKeyValue())
|
||||
}
|
||||
scope.Err(newDB.Save(elem).Error)
|
||||
scope.SetColumn(field.Name, destValue.Interface())
|
||||
|
|
4
field.go
4
field.go
|
@ -6,7 +6,7 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
type joinTable struct {
|
||||
type relationship struct {
|
||||
joinTable string
|
||||
foreignKey string
|
||||
associationForeignKey string
|
||||
|
@ -23,7 +23,7 @@ type Field struct {
|
|||
BeforeAssociation bool
|
||||
AfterAssociation bool
|
||||
isPrimaryKey bool
|
||||
JoinTable *joinTable
|
||||
Relationship *relationship
|
||||
}
|
||||
|
||||
func (f *Field) IsScanner() bool {
|
||||
|
|
2
main.go
2
main.go
|
@ -366,7 +366,7 @@ func (s *DB) Association(column string) *Association {
|
|||
scopeType := scope.IndirectValue().Type()
|
||||
if f, ok := scopeType.FieldByName(SnakeToUpperCamel(column)); ok {
|
||||
field = scope.fieldFromStruct(f)
|
||||
if field.JoinTable == nil || field.JoinTable.foreignKey == "" {
|
||||
if field.Relationship == nil || field.Relationship.foreignKey == "" {
|
||||
scope.Err(errors.New(fmt.Sprintf("invalid association %v for %v", column, scopeType)))
|
||||
}
|
||||
} else {
|
||||
|
|
8
scope.go
8
scope.go
|
@ -285,7 +285,7 @@ func (scope *Scope) fieldFromStruct(fieldStruct reflect.StructField) *Field {
|
|||
}
|
||||
|
||||
field.AfterAssociation = true
|
||||
field.JoinTable = &joinTable{
|
||||
field.Relationship = &relationship{
|
||||
joinTable: many2many,
|
||||
foreignKey: foreignKey,
|
||||
associationForeignKey: associationForeignKey,
|
||||
|
@ -294,17 +294,17 @@ func (scope *Scope) fieldFromStruct(fieldStruct reflect.StructField) *Field {
|
|||
case reflect.Struct:
|
||||
if !field.IsTime() && !field.IsScanner() {
|
||||
if foreignKey == "" && scope.HasColumn(field.Name+"Id") {
|
||||
field.JoinTable = &joinTable{foreignKey: field.Name + "Id"}
|
||||
field.Relationship = &relationship{foreignKey: field.Name + "Id"}
|
||||
field.BeforeAssociation = true
|
||||
} else if scope.HasColumn(foreignKey) {
|
||||
field.JoinTable = &joinTable{foreignKey: foreignKey}
|
||||
field.Relationship = &relationship{foreignKey: foreignKey}
|
||||
field.BeforeAssociation = true
|
||||
} else {
|
||||
if foreignKey == "" {
|
||||
foreignKey = scopeTyp.Name() + "Id"
|
||||
}
|
||||
if reflect.New(typ).Elem().FieldByName(foreignKey).IsValid() {
|
||||
field.JoinTable = &joinTable{foreignKey: foreignKey}
|
||||
field.Relationship = &relationship{foreignKey: foreignKey}
|
||||
}
|
||||
field.AfterAssociation = true
|
||||
}
|
||||
|
|
|
@ -419,20 +419,20 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
|
|||
scopeType := scope.IndirectValue().Type()
|
||||
if f, ok := scopeType.FieldByName(SnakeToUpperCamel(foreignKey)); ok {
|
||||
field := scope.fieldFromStruct(f)
|
||||
joinTable := field.JoinTable
|
||||
if joinTable != nil && joinTable.foreignKey != "" {
|
||||
foreignKey = joinTable.foreignKey
|
||||
relationship := field.Relationship
|
||||
if relationship != nil && relationship.foreignKey != "" {
|
||||
foreignKey = relationship.foreignKey
|
||||
|
||||
// many to many relations
|
||||
if joinTable.joinTable != "" {
|
||||
if relationship.joinTable != "" {
|
||||
joinSql := fmt.Sprintf(
|
||||
"INNER JOIN %v ON %v.%v = %v.%v",
|
||||
scope.Quote(joinTable.joinTable),
|
||||
scope.Quote(joinTable.joinTable),
|
||||
scope.Quote(ToSnake(joinTable.associationForeignKey)),
|
||||
scope.Quote(relationship.joinTable),
|
||||
scope.Quote(relationship.joinTable),
|
||||
scope.Quote(ToSnake(relationship.associationForeignKey)),
|
||||
toScope.QuotedTableName(),
|
||||
scope.Quote(toScope.PrimaryKey()))
|
||||
whereSql := fmt.Sprintf("%v.%v = ?", scope.Quote(joinTable.joinTable), scope.Quote(ToSnake(joinTable.foreignKey)))
|
||||
whereSql := fmt.Sprintf("%v.%v = ?", scope.Quote(relationship.joinTable), scope.Quote(ToSnake(relationship.foreignKey)))
|
||||
toScope.db.Joins(joinSql).Where(whereSql, scope.PrimaryKeyValue()).Find(value)
|
||||
return scope
|
||||
}
|
||||
|
@ -451,20 +451,20 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
|
|||
return toScope.inlineCondition(sql, scope.PrimaryKeyValue()).callCallbacks(scope.db.parent.callback.queries)
|
||||
}
|
||||
}
|
||||
scope.Err(errors.New(fmt.Sprintf("invalid association %v", foreignKeys)))
|
||||
scope.Err(fmt.Errorf("invalid association %v", foreignKeys))
|
||||
return scope
|
||||
}
|
||||
|
||||
func (scope *Scope) createJoinTable(field *Field) {
|
||||
if field.JoinTable != nil && field.JoinTable.joinTable != "" {
|
||||
if !scope.Dialect().HasTable(scope, field.JoinTable.joinTable) {
|
||||
if field.Relationship != nil && field.Relationship.joinTable != "" {
|
||||
if !scope.Dialect().HasTable(scope, field.Relationship.joinTable) {
|
||||
newScope := scope.db.NewScope("")
|
||||
primaryKeySqlType := scope.Dialect().SqlTag(reflect.ValueOf(scope.PrimaryKeyValue()), 255)
|
||||
newScope.Raw(fmt.Sprintf("CREATE TABLE %v (%v)",
|
||||
field.JoinTable.joinTable,
|
||||
field.Relationship.joinTable,
|
||||
strings.Join([]string{
|
||||
scope.Quote(ToSnake(field.JoinTable.foreignKey)) + " " + primaryKeySqlType,
|
||||
scope.Quote(ToSnake(field.JoinTable.associationForeignKey)) + " " + primaryKeySqlType}, ",")),
|
||||
scope.Quote(ToSnake(field.Relationship.foreignKey)) + " " + primaryKeySqlType,
|
||||
scope.Quote(ToSnake(field.Relationship.associationForeignKey)) + " " + primaryKeySqlType}, ",")),
|
||||
).Exec()
|
||||
scope.Err(newScope.db.Error)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue