Repository navigation
Conversation
Align orm.Query with db.Query by adding Increment(column, value...)
and Decrement(column, value...) with the same signatures. The amount
defaults to 1 and the column is quoted by the grammar
(UPDATE ... SET `votes` = `votes` + ?).
Semantics: both are built on top of Update, so they behave like any
other ORM update instead of the raw db.Query version:
- Conditions, global scopes and soft deletes are applied, so
Where(...).Increment(...) only touches the rows Get() would return.
WithoutGlobalScopes() and WithTrashed() lift them as usual.
- updated_at is touched (GORM auto-update time), like any ORM update.
- Events: like Update, Saving/Updating/Updated/Saved are fired only
when the query has a model with a primary key, e.g.
Model(&user).Increment("votes"). Query-level increments such as
Model(&User{}).Where(...).Increment(...) fire no events, matching
Laravel's query builder. WithoutEvents() is the equivalent of
Laravel's incrementQuietly.
- Model(&user).Increment(...) also syncs the field of the loaded struct
in memory (and its UpdatedAt), as Laravel does, but only when a row
was actually updated and no event replaced the expression.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1578 +/- ##
==========================================
+ Coverage 72.85% 73.49% +0.63%
==========================================
Files 412 412
Lines 26820 26860 +40
==========================================
+ Hits 19541 19742 +201
+ Misses 7278 7117 -161
Partials 1 1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
JGamboa
marked this pull request as ready for review
October 18, 2026 23:47
Contributor
Author
|
Review Ready |
…at/orm-query-increment # Conflicts: # database/gorm/query_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📑 Description
Part of goravel/goravel#1000 (section D). Follows #1575. Roadmap: goravel/goravel#1015
Adds
Increment(column string, value ...uint64) errorandDecrement(...)toorm.Query, with the same signatures asdb.Query. The amount defaults to 1, and the column is quoted by the grammar (UPDATE ... SET `votes` = `votes` + ?).Semantics. Both are built on
Update, so they behave like any other ORM update rather than the rawdb.Queryversion:WithoutGlobalScopes()andWithTrashed()lift them as usual.updated_atis touched.Update,Saving/Updating/Updated/Savedfire only when the query has a model with a primary key (Model(&user).Increment(...)).WithoutEvents()is the equivalent ofincrementQuietly.Model(&user).Increment(...)also syncs the field of the loaded struct and itsUpdatedAt, as Laravel does. This only happens when a row was actually updated and no event replaced the expression.Happy to change any of these choices if you prefer them closer to the DB module.
✅ Checks
TestIncrementAndDecrementandTestIncrementAndDecrementTouchUpdatedAt.TestIncrementAndDecrement, covering conditions,Table, a loaded model withupdated_at, and soft deletes. It passes on Postgres, MySQL, SQLite and SQL Server.