Merge pull request #300 from jnfeinstein/master

.Count() should always use ToSnake'd foreign keys.
This commit is contained in:
Jinzhu 2014-11-26 17:12:16 +08:00
commit 13c1b44804
1 changed files with 2 additions and 5 deletions

View File

@ -170,15 +170,12 @@ func (association *Association) Count() int {
relationship.JoinTable,
scope.Quote(ToSnake(relationship.ForeignKey)))
scope.db.Model("").Table(newScope.QuotedTableName()).Where(whereSql, association.PrimaryKey).Count(&count)
} else if relationship.Kind == "has_many" {
} else if relationship.Kind == "has_many" || relationship.Kind == "has_one" {
whereSql := fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(ToSnake(relationship.ForeignKey)))
scope.db.Model("").Table(newScope.QuotedTableName()).Where(whereSql, association.PrimaryKey).Count(&count)
} else if relationship.Kind == "has_one" {
whereSql := fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), relationship.ForeignKey)
scope.db.Model("").Table(newScope.QuotedTableName()).Where(whereSql, association.PrimaryKey).Count(&count)
} else if relationship.Kind == "belongs_to" {
if v, err := scope.FieldValueByName(association.Column); err == nil {
whereSql := fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), relationship.ForeignKey)
whereSql := fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(ToSnake(relationship.ForeignKey)))
scope.db.Model("").Table(newScope.QuotedTableName()).Where(whereSql, v).Count(&count)
}
}