nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: go-gorm/gorm
Pull request #4343 (2021-05-01 13:11:35)

fixed belongs_to & has_one reversed if field same
This PR is for the issue  #4316 (belongs_to and has_one reversed if field names are identical). 
It fixes the issue which was introduced in [`628a0ae`](https://github.com/go-gorm/gorm/commit/628a0ae707f230c67bca2e632fb302037c707705) commit.

- Added the correct conditions necessary to `reguessOrErr`.
- Added New Test case which handles the use-case that was failing.
- Added a utility function needed.

paraswaykole: @jinzhu Please take a look at the PR and let me know :)

Patch:
diff --git a/schema/relationship.go b/schema/relationship.go
index fee96cb..b2d485d 100644
--- a/schema/relationship.go
+++ b/schema/relationship.go
@@ -7,6 +7,7 @@ import (
 
 	"github.com/jinzhu/inflection"
 	"gorm.io/gorm/clause"
+	"gorm.io/gorm/utils"
 )
 
 // RelationshipType relationship type
@@ -404,11 +405,14 @@ func (schema *Schema) guessRelation(relation *Relationship, field *Field, cgl gu
 
 	if len(relation.foreignKeys) > 0 {
 		for _, foreignKey := range relation.foreignKeys {
-			if f := foreignSchema.LookUpField(foreignKey); f != nil {
-				foreignFields = append(foreignFields, f)
-			} else {
+			ff := foreignSchema.LookUpField(foreignKey)
+			pf := primarySchema.LookUpField(foreignKey)
+			isKeySame := utils.ExistsIn(foreignKey, &relation.primaryKeys)
+			if ff == nil || (pf != nil && ff != nil && schema == primarySchema && primarySchema != foreignSchema && !isKeySame) {
 				reguessOrErr()
 				return
+			} else {
+				foreignFields = append(foreignFields, ff)
 			}
 		}
 	} else {
diff --git a/utils/utils.go b/utils/utils.go
index ecba7fb..ce6f35d 100644
--- a/utils/utils.go
+++ b/utils/utils.go
@@ -111,3 +111,15 @@ func ToString(value interface{}) string {
 	}
 	return ""
 }
+
+func ExistsIn(a string, list *[]string) bool {
+	if list == nil {
+		return false
+	}
+	for _, b := range *list {
+		if b == a {
+			return true
+		}
+	}
+	return false
+}


Test patch:
diff --git a/schema/relationship_test.go b/schema/relationship_test.go
index 2971698..391e3a2 100644
--- a/schema/relationship_test.go
+++ b/schema/relationship_test.go
@@ -482,3 +482,22 @@ func TestSameForeignKey(t *testing.T) {
 		},
 	)
 }
+
+func TestBelongsToWithSameForeignKey(t *testing.T) {
+	type Profile struct {
+		gorm.Model
+		Name         string
+		ProfileRefer int
+	}
+
+	type User struct {
+		gorm.Model
+		Profile      Profile `gorm:"ForeignKey:ProfileRefer"`
+		ProfileRefer int
+	}
+
+	checkStructRelation(t, &User{}, Relation{
+		Name: "Profile", Type: schema.BelongsTo, Schema: "User", FieldSchema: "Profile",
+		References: []Reference{{"ID", "Profile", "ProfileRefer", "User", "", false}},
+	})
+}
Repository: go-gorm/gorm
Pull request #4387 (2021-05-17 18:37:45)

fixed has_many stopped working if field names are identical
This PR is for the issue #4386 (has_many stopped working if field names are identical).
It fixes the issue which was introduced in [`a222c75`](https://github.com/go-gorm/gorm/pull/4343/commits/a222c758bb1444d192d92c17be13721a8ede0725) commit.

- Corrected the condition.
- Added New Test case which handles the use-case that was failing.

paraswaykole: @jinzhu Please take a look at the PR
jinzhu: test still pass w/o the change.
paraswaykole: @jinzhu I added a new test `TestHasManySameForeignKey` which fails w/o the change. Just tested again. This is when there is has_many relationship with identical names as reported in #4386. I request you to please test again :)

Patch:
diff --git a/schema/relationship.go b/schema/relationship.go
index b2d485d..62256c2 100644
--- a/schema/relationship.go
+++ b/schema/relationship.go
@@ -408,7 +408,7 @@ func (schema *Schema) guessRelation(relation *Relationship, field *Field, cgl gu
 			ff := foreignSchema.LookUpField(foreignKey)
 			pf := primarySchema.LookUpField(foreignKey)
 			isKeySame := utils.ExistsIn(foreignKey, &relation.primaryKeys)
-			if ff == nil || (pf != nil && ff != nil && schema == primarySchema && primarySchema != foreignSchema && !isKeySame) {
+			if ff == nil || (pf != nil && ff != nil && schema == primarySchema && primarySchema != foreignSchema && !isKeySame && field.IndirectFieldType.Kind() == reflect.Struct) {
 				reguessOrErr()
 				return
 			} else {


Test patch:
diff --git a/schema/relationship_test.go b/schema/relationship_test.go
index 391e3a2..d0ffc28 100644
--- a/schema/relationship_test.go
+++ b/schema/relationship_test.go
@@ -501,3 +501,22 @@ func TestBelongsToWithSameForeignKey(t *testing.T) {
 		References: []Reference{{"ID", "Profile", "ProfileRefer", "User", "", false}},
 	})
 }
+
+func TestHasManySameForeignKey(t *testing.T) {
+	type Profile struct {
+		gorm.Model
+		Name      string
+		UserRefer uint
+	}
+
+	type User struct {
+		gorm.Model
+		UserRefer uint
+		Profile   []Profile `gorm:"ForeignKey:UserRefer"`
+	}
+
+	checkStructRelation(t, &User{}, Relation{
+		Name: "Profile", Type: schema.HasMany, Schema: "User", FieldSchema: "Profile",
+		References: []Reference{{"ID", "User", "UserRefer", "Profile", "", true}},
+	})
+}
Repository: go-gorm/gorm
Pull request #4524 (2021-07-14 07:47:51)

New Comma Expression
<!--
Make sure these boxes checked before submitting your pull request.

For significant changes, please open an issue to make an agreement on an implementation design/plan first before starting it.
-->

- [x] Do only one thing
- [x] Non breaking API changes
- [x] Tested

### What did this pull request do?

<!--
provide a general description of the code changes in your pull request
-->
add comma expression which produces expressions separated by commas when Build called

### User Case Description

<!-- Your use case -->
```golang
clause.CommaExpression{
	[]clause.Expression{
		clause.NamedExpr{"?", []interface{}{clause.Column{Name: "id"}}},
		clause.NamedExpr{"?", []interface{}{clause.Column{Name: "name"}}},
		clause.NamedExpr{"LENGTH(?)", []interface{}{clause.Column{Name: "mobile"}}},
	},
}
// represents: 
//`id`, `name`, LENGTH(`mobile`) 
```



Patch:
diff --git a/clause/select.go b/clause/select.go
index b93b876..d8e9f80 100644
--- a/clause/select.go
+++ b/clause/select.go
@@ -43,3 +43,17 @@ func (s Select) MergeClause(clause *Clause) {
 		clause.Expression = s
 	}
 }
+
+// CommaExpression represents a group of expressions separated by commas.
+type CommaExpression struct {
+	Exprs []Expression
+}
+
+func (comma CommaExpression) Build(builder Builder) {
+	for idx, expr := range comma.Exprs {
+		if idx > 0 {
+			_, _ = builder.WriteString(", ")
+		}
+		expr.Build(builder)
+	}
+}


Test patch:
diff --git a/clause/select_test.go b/clause/select_test.go
index b729643..9fce078 100644
--- a/clause/select_test.go
+++ b/clause/select_test.go
@@ -31,6 +31,18 @@ func TestSelect(t *testing.T) {
 			}, clause.From{}},
 			"SELECT `name` FROM `users`", nil,
 		},
+		{
+			[]clause.Interface{clause.Select{
+				Expression: clause.CommaExpression{
+					Exprs: []clause.Expression{
+						clause.NamedExpr{"?", []interface{}{clause.Column{Name: "id"}}},
+						clause.NamedExpr{"?", []interface{}{clause.Column{Name: "name"}}},
+						clause.NamedExpr{"LENGTH(?)", []interface{}{clause.Column{Name: "mobile"}}},
+					},
+				},
+			}, clause.From{}},
+			"SELECT `id`, `name`, LENGTH(`mobile`) FROM `users`", nil,
+		},
 	}
 
 	for idx, result := range results {
Repository: go-gorm/gorm
Pull request #4621 (2021-08-18 14:00:31)

feat: QuoteTo accept clause.Expr
<!--
Make sure these boxes checked before submitting your pull request.

For significant changes, please open an issue to make an agreement on an implementation design/plan first before starting it.
-->

- [x] Do only one thing
- [x] Non breaking API changes
- [x] Tested

### What did this pull request do?

`Statement.QuoteTo` accept `clause.Expr` as parameter
<!--
provide a general description of the code changes in your pull request
-->

### User Case Description

<!-- Your use case -->
```go
s.WriteQuoted(clause.Expr{SQL: "SUM(?)", Vars: []interface{}{clause.Column{Table: "users", Name: "id"}}})
```



Patch:
diff --git a/statement.go b/statement.go
index 8b682c8..93b78c1 100644
--- a/statement.go
+++ b/statement.go
@@ -129,6 +129,8 @@ func (stmt *Statement) QuoteTo(writer clause.Writer, field interface{}) {
 			stmt.QuoteTo(writer, d)
 		}
 		writer.WriteByte(')')
+	case clause.Expr:
+		v.Build(stmt)
 	case string:
 		stmt.DB.Dialector.QuoteTo(writer, v)
 	case []string:


Test patch:
diff --git a/clause/expression_test.go b/clause/expression_test.go
index 0ccd077..0507486 100644
--- a/clause/expression_test.go
+++ b/clause/expression_test.go
@@ -156,6 +156,18 @@ func TestExpression(t *testing.T) {
 		},
 		ExpectedVars: []interface{}{"a", "b"},
 		Result:       "`column-name` NOT IN (?,?)",
+	}, {
+		Expressions: []clause.Expression{
+			clause.Eq{Column: clause.Expr{SQL: "SUM(?)", Vars: []interface{}{clause.Column{Name: "id"}}}, Value: 100},
+		},
+		ExpectedVars: []interface{}{100},
+		Result:       "SUM(`id`) = ?",
+	}, {
+		Expressions: []clause.Expression{
+			clause.Gte{Column: clause.Expr{SQL: "SUM(?)", Vars: []interface{}{clause.Column{Table: "users", Name: "id"}}}, Value: 100},
+		},
+		ExpectedVars: []interface{}{100},
+		Result:       "SUM(`users`.`id`) >= ?",
 	}}
 
 	for idx, result := range results {