mirror of https://github.com/go-gorm/gorm.git
Passed all tests for multiple primary keys
This commit is contained in:
parent
f50956cfef
commit
f00b95d305
|
@ -174,7 +174,7 @@ func (association *Association) Count() int {
|
||||||
if relationship.PolymorphicType != "" {
|
if relationship.PolymorphicType != "" {
|
||||||
query = query.Where(fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(relationship.PolymorphicDBName)), scope.TableName())
|
query = query.Where(fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(relationship.PolymorphicDBName)), scope.TableName())
|
||||||
}
|
}
|
||||||
query.Count(&count)
|
query.Table(newScope.TableName()).Count(&count)
|
||||||
} else if relationship.Kind == "belongs_to" {
|
} else if relationship.Kind == "belongs_to" {
|
||||||
query := scope.DB()
|
query := scope.DB()
|
||||||
for idx, foreignKey := range relationship.ForeignDBNames {
|
for idx, foreignKey := range relationship.ForeignDBNames {
|
||||||
|
|
|
@ -195,6 +195,8 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
if polymorphicType := getForeignField(polymorphic+"Type", toScope.GetStructFields()); polymorphicType != nil {
|
if polymorphicType := getForeignField(polymorphic+"Type", toScope.GetStructFields()); polymorphicType != nil {
|
||||||
relationship.ForeignFieldNames = []string{polymorphicField.Name}
|
relationship.ForeignFieldNames = []string{polymorphicField.Name}
|
||||||
relationship.ForeignDBNames = []string{polymorphicField.DBName}
|
relationship.ForeignDBNames = []string{polymorphicField.DBName}
|
||||||
|
relationship.AssociationForeignFieldNames = []string{scope.PrimaryField().Name}
|
||||||
|
relationship.AssociationForeignDBNames = []string{scope.PrimaryField().DBName}
|
||||||
relationship.PolymorphicType = polymorphicType.Name
|
relationship.PolymorphicType = polymorphicType.Name
|
||||||
relationship.PolymorphicDBName = polymorphicType.DBName
|
relationship.PolymorphicDBName = polymorphicType.DBName
|
||||||
polymorphicType.IsForeignKey = true
|
polymorphicType.IsForeignKey = true
|
||||||
|
@ -300,8 +302,8 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
if len(foreignKeys) == 0 {
|
if len(foreignKeys) == 0 {
|
||||||
for _, f := range toScope.PrimaryFields() {
|
for _, f := range scope.PrimaryFields() {
|
||||||
if foreignField := getForeignField(field.Name+f.Name, fields); foreignField != nil {
|
if foreignField := getForeignField(modelStruct.ModelType.Name()+f.Name, toScope.GetStructFields()); foreignField != nil {
|
||||||
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, f.Name)
|
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, f.Name)
|
||||||
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, f.DBName)
|
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, f.DBName)
|
||||||
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
|
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
|
||||||
|
@ -311,9 +313,9 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for _, foreignKey := range foreignKeys {
|
for _, foreignKey := range foreignKeys {
|
||||||
if foreignField := getForeignField(foreignKey, fields); foreignField != nil {
|
if foreignField := getForeignField(foreignKey, toScope.GetStructFields()); foreignField != nil {
|
||||||
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, toScope.PrimaryField().Name)
|
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, scope.PrimaryField().Name)
|
||||||
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, toScope.PrimaryField().DBName)
|
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
|
||||||
|
@ -322,12 +324,12 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(relationship.ForeignFieldNames) != 0 {
|
if len(relationship.ForeignFieldNames) != 0 {
|
||||||
relationship.Kind = "belongs_to"
|
relationship.Kind = "has_one"
|
||||||
field.Relationship = relationship
|
field.Relationship = relationship
|
||||||
} else {
|
} else {
|
||||||
if len(foreignKeys) == 0 {
|
if len(foreignKeys) == 0 {
|
||||||
for _, f := range scope.PrimaryFields() {
|
for _, f := range toScope.PrimaryFields() {
|
||||||
if foreignField := getForeignField(modelStruct.ModelType.Name()+f.Name, toScope.GetStructFields()); foreignField != nil {
|
if foreignField := getForeignField(field.Name+f.Name, fields); foreignField != nil {
|
||||||
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, f.Name)
|
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, f.Name)
|
||||||
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, f.DBName)
|
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, f.DBName)
|
||||||
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
|
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
|
||||||
|
@ -337,9 +339,9 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for _, foreignKey := range foreignKeys {
|
for _, foreignKey := range foreignKeys {
|
||||||
if foreignField := getForeignField(foreignKey, toScope.GetStructFields()); foreignField != nil {
|
if foreignField := getForeignField(foreignKey, fields); foreignField != nil {
|
||||||
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, scope.PrimaryField().Name)
|
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, toScope.PrimaryField().Name)
|
||||||
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, scope.PrimaryField().DBName)
|
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, toScope.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
|
||||||
|
@ -348,7 +350,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(relationship.ForeignFieldNames) != 0 {
|
if len(relationship.ForeignFieldNames) != 0 {
|
||||||
relationship.Kind = "has_one"
|
relationship.Kind = "belongs_to"
|
||||||
field.Relationship = relationship
|
field.Relationship = relationship
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue