Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix cond in scopes #6152

Merged
merged 3 commits into from Apr 11, 2023
Merged

fix cond in scopes #6152

merged 3 commits into from Apr 11, 2023

Conversation

black-06
Copy link
Contributor

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

fix conditions in Scope #6148

User Case Description

DB.Table("test").Scopes(
	func(d *gorm.DB) *gorm.DB {
		return d.Where("a = 1")
	},
	func(d *gorm.DB) *gorm.DB {
		return d.Where(d.Or("b = 2").Or("c = 3"))
	},
).Rows()

It should be:

SELECT * FROM `test` WHERE a = 1 AND (b = 2 OR c = 3)

But now it is:

SELECT * FROM `test` WHERE a = 1 OR b = 2 OR c = 3 AND (a = 1 OR b = 2 OR c = 3)

func (db *DB) executeScopes(keepScopes bool) (tx *DB) {
tx = db.getInstance()
scopes := db.Statement.scopes
if len(scopes) == 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in a scope condition, it might have other scope conditions, so we are writing the code like this:

for len(db.Statement.scopes) > 0 {
	db = db.executeScopes(false)
		scopes := db.Statement.scopes
		db.Statement.scopes = nil
		for _, scope := range scopes {
			db = scope(db)
		}
	}

Seems like the change will break it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, it's my fault.

@black-06 black-06 requested a review from jinzhu March 23, 2023 05:01
@jinzhu jinzhu merged commit 4b0da0e into go-gorm:master Apr 11, 2023
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants