Skip to content

feat: [#1000] add Latest, Value and DoesntExist to orm.Query - #1577

Open
JGamboa wants to merge 3 commits into
goravel:masterfrom
JGamboa:feat/orm-query-results
Open

JGamboa wants to merge 3 commits into
goravel:masterfrom
JGamboa:feat/orm-query-results

Conversation

@JGamboa

@JGamboa JGamboa commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

📑 Description

Part of goravel/goravel#1000 (reading results section). Follows #1575. Roadmap: goravel/goravel#1015

Adds Latest, Value and DoesntExist to orm.Query, with the same signatures as db.Query:

facades.Orm().Query().Model(&Post{}).Latest().First(&post)                        // ORDER BY created_at DESC
facades.Orm().Query().Model(&User{}).Where("id", 1).Value("email", &email)
missing, err := facades.Orm().Query().Model(&User{}).Where("email", email).DoesntExist()
  • Latest(column ...string) is OrderByDesc, defaulting to created_at when no column (or an empty one) is given.
  • DoesntExist() is !Exists(), so it respects global scopes and soft deletes.
  • Value(column, dest):
    • It applies global scopes and conditions, then Select(column).Limit(1).Scan(dest).
    • A non-pointer dest returns DatabaseUnsupportedType.
    • With no rows it returns nil and leaves dest untouched, like the DB module.
    • It doesn't fire Retrieved, since it reads a raw column, like Pluck/Sum.
  • Mocks are regenerated.

Found while testing, not changed here: OrderByDesc("id").First() (and so Latest("id").First()) fails on SQL Server, because GORM appends a second ORDER BY id and the column ends up duplicated. It happens on master too. I can open an issue for it.

✅ Checks

  • Added test cases for my code
    • Unit tests in database/gorm.
    • Integration: TestLatest, TestValue, TestDoesntExist, plus DoesntExist/Value subtests in TestGlobalScopes. They pass on Postgres, MySQL, SQLite and SQL Server.

@codecov

codecov Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.27%. Comparing base (7ffe4a8) to head (6306648).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1577      +/-   ##
==========================================
+ Coverage   72.91%   73.27%   +0.36%     
==========================================
  Files         412      412              
  Lines       26893    26906      +13     
==========================================
+ Hits        19608    19716     +108     
+ Misses       7283     7188      -95     
  Partials        2        2              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@JGamboa
JGamboa marked this pull request as ready for review October 18, 2026 23:47
@JGamboa
JGamboa requested a review from a team as a code owner October 18, 2026 23:47
@JGamboa

JGamboa commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

Review Ready

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.

1 participant