Gorm: Or() statement causes priority disorder

Created on 18 Oct 2020  ·  4Comments  ·  Source: go-gorm/gorm

var friends []*models.Friend
if res := f.db().Debug().Model(&models.Friend{}).Where(&models.Friend{UserA: userID}).Or(models.Friend{UserB: userID}).Find(&friends); res.Error != nil {
    logger.Error(res.Error)
    return nil, queryErr
}

The code above executes the SQL as follows:

SELECT * FROM `friends` WHERE `friends`.`user_a` = 1 OR `friends`.`user_b` = 1 AND `friends`.`deleted_at` IS NULL

which obviously has priority issues. Although it could be easily avoided, IMO most use cases here are intended as "(XX OR XX) AND deleted_at IS NULL", otherwise the Or() API is less useful and will cause unintentional mistakes.

stale missing reproduction steps

All 4 comments

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

`friends`.`user_a` = 1 OR `friends`.`user_b` = 1 AND `friends`.`deleted_at` IS NULL

equals

(`friends`.`user_a` = 1 OR `friends`.`user_b` = 1) AND `friends`.`deleted_at` IS NULL
`friends`.`user_a` = 1 OR `friends`.`user_b` = 1 AND `friends`.`deleted_at` IS NULL

equals

(`friends`.`user_a` = 1 OR `friends`.`user_b` = 1) AND `friends`.`deleted_at` IS NULL

No, it does't.

Hi @voe1999

Sorry for the issue, there was an issue when using soft delete with or conditions, just pushed a commit to fix the issue.

Thank you for your report.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sredxny picture sredxny  ·  3Comments

alanyuen picture alanyuen  ·  3Comments

rfyiamcool picture rfyiamcool  ·  3Comments

leebrooks0 picture leebrooks0  ·  3Comments

koalacxr picture koalacxr  ·  3Comments