Fix compile error

This commit is contained in:
Jinzhu 2015-02-16 16:42:01 +08:00
parent 3a73206010
commit f4e705b749
3 changed files with 13 additions and 15 deletions

View File

@ -98,8 +98,8 @@ func (association *Association) Delete(values ...interface{}) *Association {
// many to many // many to many
if relationship.Kind == "many_to_many" { if relationship.Kind == "many_to_many" {
whereSql := fmt.Sprintf("%v.%v = ? AND %v.%v IN (?)", whereSql := fmt.Sprintf("%v.%v = ? AND %v.%v IN (?)",
relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.ForeignKey)), relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.ForeignFieldName)),
relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.AssociationForeignKey))) relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.AssociationForeignFieldName)))
association.Scope.db.Model("").Table(relationship.JoinTable). association.Scope.db.Model("").Table(relationship.JoinTable).
Where(whereSql, association.PrimaryKey, primaryKeys).Delete("") 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 (?)", 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.ForeignFieldName)),
relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.AssociationForeignKey))) relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.AssociationForeignFieldName)))
scope.db.Model("").Table(relationship.JoinTable).Where(whereSql, association.PrimaryKey, addedPrimaryKeys).Delete("") scope.db.Model("").Table(relationship.JoinTable).Where(whereSql, association.PrimaryKey, addedPrimaryKeys).Delete("")
} else { } else {
@ -152,7 +152,7 @@ func (association *Association) Clear() *Association {
relationship := association.Field.Relationship relationship := association.Field.Relationship
scope := association.Scope scope := association.Scope
if relationship.Kind == "many_to_many" { 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("") scope.db.Model("").Table(relationship.JoinTable).Where(whereSql, association.PrimaryKey).Delete("")
} else { } else {
association.setErr(errors.New("clear only support many to many")) association.setErr(errors.New("clear only support many to many"))
@ -173,13 +173,13 @@ func (association *Association) Count() int {
newScope.QuotedTableName(), newScope.QuotedTableName(),
scope.Quote(newScope.PrimaryKey()), scope.Quote(newScope.PrimaryKey()),
relationship.JoinTable, relationship.JoinTable,
scope.Quote(ToSnake(relationship.AssociationForeignKey)), scope.Quote(ToSnake(relationship.AssociationForeignFieldName)),
relationship.JoinTable, relationship.JoinTable,
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) scope.db.Model("").Table(newScope.QuotedTableName()).Where(whereSql, association.PrimaryKey).Count(&count)
} else if relationship.Kind == "has_many" || relationship.Kind == "has_one" { } 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) countScope := scope.db.Model("").Table(newScope.QuotedTableName()).Where(whereSql, association.PrimaryKey)
if relationship.ForeignType != "" { if relationship.ForeignType != "" {
countScope = countScope.Where(fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(ToSnake(relationship.ForeignType))), association.PrimaryType) 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) countScope.Count(&count)
} else if relationship.Kind == "belongs_to" { } else if relationship.Kind == "belongs_to" {
if v, err := scope.FieldValueByName(association.Column); err == nil { 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) scope.db.Model("").Table(newScope.QuotedTableName()).Where(whereSql, v).Count(&count)
} }
} }

View File

@ -123,9 +123,8 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
if !ast.IsExported(fieldStruct.Name) { if !ast.IsExported(fieldStruct.Name) {
continue continue
} }
var field *StructField
field.Struct = fieldStruct field := &StructField{Struct: fieldStruct}
if fieldStruct.Tag.Get("sql") == "-" { if fieldStruct.Tag.Get("sql") == "-" {
field.IsIgnored = true field.IsIgnored = true
} else { } else {

View File

@ -496,7 +496,7 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
return scope return scope
} }
func (scope *Scope) createJoinTable(field *Field) { func (scope *Scope) createJoinTable(field *StructField) {
if field.Relationship != nil && field.Relationship.JoinTable != "" { if field.Relationship != nil && field.Relationship.JoinTable != "" {
if !scope.Dialect().HasTable(scope, field.Relationship.JoinTable) { if !scope.Dialect().HasTable(scope, field.Relationship.JoinTable) {
newScope := scope.db.NewScope("") newScope := scope.db.NewScope("")
@ -581,11 +581,10 @@ func (scope *Scope) autoMigrate() *Scope {
if !scope.Dialect().HasTable(scope, tableName) { if !scope.Dialect().HasTable(scope, tableName) {
scope.createTable() scope.createTable()
} else { } else {
for _, field := range scope.Fields() { for _, field := range scope.GetStructFields() {
if !scope.Dialect().HasColumn(scope, tableName, field.DBName) { if !scope.Dialect().HasColumn(scope, tableName, field.DBName) {
if field.IsNormal { if field.IsNormal {
sqlTag := scope.sqlTagForField(field) scope.Raw(fmt.Sprintf("ALTER TABLE %v ADD %v %v;", quotedTableName, field.DBName, field.SqlTag)).Exec()
scope.Raw(fmt.Sprintf("ALTER TABLE %v ADD %v %v;", quotedTableName, field.DBName, sqlTag)).Exec()
} }
} }
scope.createJoinTable(field) scope.createJoinTable(field)