forked from mirror/gorm
Fix compile error for scope_private
This commit is contained in:
parent
dc428d2364
commit
fea291e796
|
@ -256,18 +256,27 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
relationship.JoinTableHandler = &joinTableHandler
|
relationship.JoinTableHandler = &joinTableHandler
|
||||||
field.Relationship = relationship
|
field.Relationship = relationship
|
||||||
} else {
|
} else {
|
||||||
|
relationship.Kind = "has_many"
|
||||||
|
|
||||||
if len(foreignKeys) == 0 {
|
if len(foreignKeys) == 0 {
|
||||||
for _, field := range scope.PrimaryFields() {
|
for _, field := range scope.PrimaryFields() {
|
||||||
foreignKeys = append(foreignKeys, scopeType.Name()+field.Name)
|
if foreignField := getForeignField(scopeType.Name()+field.Name, toScope.GetStructFields()); foreignField != nil {
|
||||||
|
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, field.Name)
|
||||||
|
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, field.DBName)
|
||||||
|
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
|
||||||
|
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
|
||||||
|
foreignField.IsForeignKey = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
for _, foreignKey := range foreignKeys {
|
||||||
relationship.Kind = "has_many"
|
if foreignField := getForeignField(foreignKey, toScope.GetStructFields()); foreignField != nil {
|
||||||
for _, foreignKey := range foreignKeys {
|
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, scope.PrimaryField().Name)
|
||||||
if foreignField := getForeignField(foreignKey, toScope.GetStructFields()); foreignField != nil {
|
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, scope.PrimaryField().DBName)
|
||||||
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
|
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
|
||||||
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
|
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
|
||||||
foreignField.IsForeignKey = true
|
foreignField.IsForeignKey = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -293,15 +302,23 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
belongsToForeignKeys := foreignKeys
|
belongsToForeignKeys := foreignKeys
|
||||||
if len(belongsToForeignKeys) == 0 {
|
if len(belongsToForeignKeys) == 0 {
|
||||||
for _, field := range toScope.PrimaryFields() {
|
for _, field := range toScope.PrimaryFields() {
|
||||||
belongsToForeignKeys = append(belongsToForeignKeys, field.Name+field.Name)
|
if foreignField := getForeignField(field.Name+field.Name, fields); foreignField != nil {
|
||||||
|
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, field.Name)
|
||||||
|
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, field.DBName)
|
||||||
|
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
|
||||||
|
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
|
||||||
|
foreignField.IsForeignKey = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
for _, foreignKey := range foreignKeys {
|
||||||
for _, foreignKey := range belongsToForeignKeys {
|
if foreignField := getForeignField(foreignKey, fields); foreignField != nil {
|
||||||
if foreignField := getForeignField(foreignKey, fields); foreignField != nil {
|
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, toScope.PrimaryField().Name)
|
||||||
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
|
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, toScope.PrimaryField().DBName)
|
||||||
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
|
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
|
||||||
foreignField.IsForeignKey = true
|
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
|
||||||
|
foreignField.IsForeignKey = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -312,15 +329,23 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
hasOneForeignKeys := foreignKeys
|
hasOneForeignKeys := foreignKeys
|
||||||
if len(hasOneForeignKeys) == 0 {
|
if len(hasOneForeignKeys) == 0 {
|
||||||
for _, field := range toScope.PrimaryFields() {
|
for _, field := range toScope.PrimaryFields() {
|
||||||
hasOneForeignKeys = append(hasOneForeignKeys, modelStruct.ModelType.Name()+field.Name)
|
if foreignField := getForeignField(modelStruct.ModelType.Name()+field.Name, fields); foreignField != nil {
|
||||||
|
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, field.Name)
|
||||||
|
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, field.DBName)
|
||||||
|
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
|
||||||
|
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
|
||||||
|
foreignField.IsForeignKey = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
for _, foreignKey := range hasOneForeignKeys {
|
||||||
for _, foreignKey := range hasOneForeignKeys {
|
if foreignField := getForeignField(foreignKey, toScope.GetStructFields()); foreignField != nil {
|
||||||
if foreignField := getForeignField(foreignKey, toScope.GetStructFields()); foreignField != nil {
|
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, scope.PrimaryField().Name)
|
||||||
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
|
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, scope.PrimaryField().DBName)
|
||||||
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
|
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
|
||||||
foreignField.IsForeignKey = true
|
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
|
||||||
|
foreignField.IsForeignKey = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -415,12 +415,21 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
|
||||||
joinTableHandler := relationship.JoinTableHandler
|
joinTableHandler := relationship.JoinTableHandler
|
||||||
scope.Err(joinTableHandler.JoinWith(joinTableHandler, toScope.db, scope.Value).Find(value).Error)
|
scope.Err(joinTableHandler.JoinWith(joinTableHandler, toScope.db, scope.Value).Find(value).Error)
|
||||||
} else if relationship.Kind == "belongs_to" {
|
} else if relationship.Kind == "belongs_to" {
|
||||||
sql := fmt.Sprintf("%v = ?", scope.Quote(toScope.PrimaryKey()))
|
query := toScope.db
|
||||||
foreignKeyValue := fromFields[relationship.ForeignDBName].Field.Interface()
|
for idx, foreignKey := range relationship.ForeignDBNames {
|
||||||
scope.Err(toScope.db.Where(sql, foreignKeyValue).Find(value).Error)
|
if field, ok := scope.FieldByName(relationship.AssociationForeignDBNames[idx]); ok {
|
||||||
|
query = query.Where(fmt.Sprintf("%v = ?", scope.Quote(foreignKey)), field.Field.Interface())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
scope.Err(query.Find(value).Error)
|
||||||
} else if relationship.Kind == "has_many" || relationship.Kind == "has_one" {
|
} else if relationship.Kind == "has_many" || relationship.Kind == "has_one" {
|
||||||
sql := fmt.Sprintf("%v = ?", scope.Quote(relationship.ForeignDBName))
|
query := toScope.db
|
||||||
query := toScope.db.Where(sql, scope.PrimaryKeyValue())
|
for idx, foreignKey := range relationship.ForeignDBNames {
|
||||||
|
if field, ok := scope.FieldByName(relationship.AssociationForeignDBNames[idx]); ok {
|
||||||
|
query = query.Where(fmt.Sprintf("%v = ?", scope.Quote(foreignKey)), field.Field.Interface())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if relationship.PolymorphicType != "" {
|
if relationship.PolymorphicType != "" {
|
||||||
query = query.Where(fmt.Sprintf("%v = ?", scope.Quote(relationship.PolymorphicDBName)), scope.TableName())
|
query = query.Where(fmt.Sprintf("%v = ?", scope.Quote(relationship.PolymorphicDBName)), scope.TableName())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue