The code block should be limited in ("has_many", "has_one") to avoid wrong settings of other relation kinds

This commit is contained in:
Yang Qing 2019-07-05 16:11:35 +08:00
parent 836fb2c19d
commit 443f1de146

View File

@ -276,6 +276,12 @@ func (association *Association) Count() int {
fmt.Sprintf("%v IN (%v)", toQueryCondition(scope, relationship.ForeignDBNames), toQueryMarks(primaryKeys)), fmt.Sprintf("%v IN (%v)", toQueryCondition(scope, relationship.ForeignDBNames), toQueryMarks(primaryKeys)),
toQueryValues(primaryKeys)..., toQueryValues(primaryKeys)...,
) )
if relationship.PolymorphicType != "" {
query = query.Where(
fmt.Sprintf("%v.%v = ?", scope.New(fieldValue).QuotedTableName(), scope.Quote(relationship.PolymorphicDBName)),
relationship.PolymorphicValue,
)
}
case "belongs_to": case "belongs_to":
primaryKeys := scope.getColumnAsArray(relationship.ForeignFieldNames, scope.Value) primaryKeys := scope.getColumnAsArray(relationship.ForeignFieldNames, scope.Value)
query = query.Where( query = query.Where(
@ -284,13 +290,6 @@ func (association *Association) Count() int {
) )
} }
if relationship.PolymorphicType != "" {
query = query.Where(
fmt.Sprintf("%v.%v = ?", scope.New(fieldValue).QuotedTableName(), scope.Quote(relationship.PolymorphicDBName)),
relationship.PolymorphicValue,
)
}
if err := query.Model(fieldValue).Count(&count).Error; err != nil { if err := query.Model(fieldValue).Count(&count).Error; err != nil {
association.Error = err association.Error = err
} }