diff --git a/association_test.go b/association_test.go index 3ffd8880..a7b8f136 100644 --- a/association_test.go +++ b/association_test.go @@ -143,7 +143,7 @@ func TestManyToMany(t *testing.T) { // Query var newLanguages []Language - DB.Model(&user).Related(&newLanguages, "Languages") + DB.Debug().Model(&user).Related(&newLanguages, "Languages") if len(newLanguages) != len([]string{"ZH", "EN"}) { t.Errorf("Query many to many relations") } diff --git a/join_table.go b/join_table.go index 9b23f89f..d29f7bfb 100644 --- a/join_table.go +++ b/join_table.go @@ -128,7 +128,8 @@ func (s JoinTableHandler) JoinWith(db *DB, source interface{}) *DB { var values []interface{} if s.Source.ModelType == modelType { for _, foreignKey := range s.Destination.ForeignKeys { - joinConditions = append(joinConditions, fmt.Sprintf("%v.%v = %v.%v", quotedTable, scope.Quote(foreignKey.DBName), scope.QuotedTableName(), scope.Quote(foreignKey.AssociationDBName))) + destinationTableName := scope.New(reflect.New(s.Destination.ModelType).Interface()).QuotedTableName() + joinConditions = append(joinConditions, fmt.Sprintf("%v.%v = %v.%v", quotedTable, scope.Quote(foreignKey.DBName), destinationTableName, scope.Quote(foreignKey.AssociationDBName))) } for _, foreignKey := range s.Source.ForeignKeys { @@ -137,7 +138,8 @@ func (s JoinTableHandler) JoinWith(db *DB, source interface{}) *DB { } } else if s.Destination.ModelType == modelType { for _, foreignKey := range s.Source.ForeignKeys { - joinConditions = append(joinConditions, fmt.Sprintf("%v.%v = %v.%v", quotedTable, scope.Quote(foreignKey.DBName), scope.QuotedTableName(), scope.Quote(foreignKey.AssociationDBName))) + sourceTableName := scope.New(reflect.New(s.Source.ModelType).Interface()).QuotedTableName() + joinConditions = append(joinConditions, fmt.Sprintf("%v.%v = %v.%v", quotedTable, scope.Quote(foreignKey.DBName), sourceTableName, scope.Quote(foreignKey.AssociationDBName))) } for _, foreignKey := range s.Destination.ForeignKeys { @@ -146,6 +148,6 @@ func (s JoinTableHandler) JoinWith(db *DB, source interface{}) *DB { } } - return db.Joins(fmt.Sprintf("INNER JOIN %v ON %v", strings.Join(joinConditions, " AND "))). + return db.Joins(fmt.Sprintf("INNER JOIN %v ON %v", quotedTable, strings.Join(joinConditions, " AND "))). Where(strings.Join(queryConditions, " AND "), values...) }