gorm
gorm copied to clipboard
feat(Scopes): make Schema and Table accessible from scopes
- [x] Do only one thing
- [x] Non breaking API changes
- [x] Tested
What did this pull request do?
This PR executes scopes after parsing the model so Statement.Schema
and Statement.Table
are accessible from them.
User Case Description
This would be useful in order to implement scopes that are re-usable across multiple models.
An example would be a scope that sorts by ID. The following would not work if there is a join or an ambiguity with the table name:
func sortByID(db *gorm.DB) *gorm.DB) {
return db.Sort("id")
}
Thanks to the change suggested in this PR, we would be able to do the following:
func sortByID(db *gorm.DB) *gorm.DB) {
return db.Sort(fmt.Sprintf("%s.id", db.Statement.Table))
}
This would also open a lot of possibilities for automatic query generation based on a schema.