diff --git a/association.go b/association.go index 32570cf9..3acacc47 100644 --- a/association.go +++ b/association.go @@ -98,8 +98,8 @@ func (association *Association) Delete(values ...interface{}) *Association { // many to many if relationship.Kind == "many_to_many" { whereSql := fmt.Sprintf("%v.%v = ? AND %v.%v IN (?)", - relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.ForeignKey)), - relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.AssociationForeignKey))) + relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.ForeignFieldName)), + relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.AssociationForeignFieldName))) association.Scope.db.Model("").Table(relationship.JoinTable). Where(whereSql, association.PrimaryKey, primaryKeys).Delete("") @@ -138,8 +138,8 @@ func (association *Association) Replace(values ...interface{}) *Association { } whereSql := fmt.Sprintf("%v.%v = ? AND %v.%v NOT IN (?)", - relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.ForeignKey)), - relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.AssociationForeignKey))) + relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.ForeignFieldName)), + relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.AssociationForeignFieldName))) scope.db.Model("").Table(relationship.JoinTable).Where(whereSql, association.PrimaryKey, addedPrimaryKeys).Delete("") } else { @@ -152,7 +152,7 @@ func (association *Association) Clear() *Association { relationship := association.Field.Relationship scope := association.Scope if relationship.Kind == "many_to_many" { - whereSql := fmt.Sprintf("%v.%v = ?", relationship.JoinTable, scope.Quote(ToSnake(relationship.ForeignKey))) + whereSql := fmt.Sprintf("%v.%v = ?", relationship.JoinTable, scope.Quote(ToSnake(relationship.ForeignFieldName))) scope.db.Model("").Table(relationship.JoinTable).Where(whereSql, association.PrimaryKey).Delete("") } else { association.setErr(errors.New("clear only support many to many")) @@ -173,13 +173,13 @@ func (association *Association) Count() int { newScope.QuotedTableName(), scope.Quote(newScope.PrimaryKey()), relationship.JoinTable, - scope.Quote(ToSnake(relationship.AssociationForeignKey)), + scope.Quote(ToSnake(relationship.AssociationForeignFieldName)), relationship.JoinTable, relationship.JoinTable, - scope.Quote(ToSnake(relationship.ForeignKey))) + scope.Quote(ToSnake(relationship.ForeignFieldName))) scope.db.Model("").Table(newScope.QuotedTableName()).Where(whereSql, association.PrimaryKey).Count(&count) } else if relationship.Kind == "has_many" || relationship.Kind == "has_one" { - whereSql := fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(ToSnake(relationship.ForeignKey))) + whereSql := fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(ToSnake(relationship.ForeignFieldName))) countScope := scope.db.Model("").Table(newScope.QuotedTableName()).Where(whereSql, association.PrimaryKey) if relationship.ForeignType != "" { countScope = countScope.Where(fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(ToSnake(relationship.ForeignType))), association.PrimaryType) @@ -187,7 +187,7 @@ func (association *Association) Count() int { countScope.Count(&count) } else if relationship.Kind == "belongs_to" { if v, err := scope.FieldValueByName(association.Column); err == nil { - whereSql := fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(ToSnake(relationship.ForeignKey))) + whereSql := fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(ToSnake(relationship.ForeignFieldName))) scope.db.Model("").Table(newScope.QuotedTableName()).Where(whereSql, v).Count(&count) } } diff --git a/model_struct.go b/model_struct.go index af062567..9da1789d 100644 --- a/model_struct.go +++ b/model_struct.go @@ -123,9 +123,8 @@ func (scope *Scope) GetModelStruct() *ModelStruct { if !ast.IsExported(fieldStruct.Name) { continue } - var field *StructField - field.Struct = fieldStruct + field := &StructField{Struct: fieldStruct} if fieldStruct.Tag.Get("sql") == "-" { field.IsIgnored = true } else { diff --git a/scope_private.go b/scope_private.go index 8473fab9..63263527 100644 --- a/scope_private.go +++ b/scope_private.go @@ -496,7 +496,7 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope { return scope } -func (scope *Scope) createJoinTable(field *Field) { +func (scope *Scope) createJoinTable(field *StructField) { if field.Relationship != nil && field.Relationship.JoinTable != "" { if !scope.Dialect().HasTable(scope, field.Relationship.JoinTable) { newScope := scope.db.NewScope("") @@ -581,11 +581,10 @@ func (scope *Scope) autoMigrate() *Scope { if !scope.Dialect().HasTable(scope, tableName) { scope.createTable() } else { - for _, field := range scope.Fields() { + for _, field := range scope.GetStructFields() { if !scope.Dialect().HasColumn(scope, tableName, field.DBName) { if field.IsNormal { - sqlTag := scope.sqlTagForField(field) - scope.Raw(fmt.Sprintf("ALTER TABLE %v ADD %v %v;", quotedTableName, field.DBName, sqlTag)).Exec() + scope.Raw(fmt.Sprintf("ALTER TABLE %v ADD %v %v;", quotedTableName, field.DBName, field.SqlTag)).Exec() } } scope.createJoinTable(field)