mirror of https://github.com/go-gorm/gorm.git
Merge pull request #300 from jnfeinstein/master
.Count() should always use ToSnake'd foreign keys.
This commit is contained in:
commit
13c1b44804
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue