Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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
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.

3 participants