Skip to content

Fix/join single presFix QueryBuilder JOIN state when using singleerve joins - #2578

Merged
an-tao merged 2 commits into
drogonframework:masterfrom
ljcjclljc:fix/join-single-preserve-joins
Sep 3, 2026
Merged

an-tao merged 2 commits into
drogonframework:masterfrom
ljcjclljc:fix/join-single-preserve-joins

Conversation

@ljcjclljc

Copy link
Copy Markdown
Contributor

Fix QueryBuilder JOIN state when using single

Summary

  • Preserve JOIN clauses when converting a QueryBuilder to a single-result builder.
  • Add regression tests for model and Row results with LEFT JOIN + single().
  • Cover the PostgreSQL, MySQL, and SQLite test sections.

Problem

TransformBuilder::single() converts a non-single builder into a single-result builder. The conversion constructor copied the existing query state except for joins_.

As a result, a query such as:

QueryBuilder<Users>{}
    .from("users")
    .select("wallets.amount")
    .leftJoin("wallets", "users.user_id", "wallets.user_id")
    .limit(1)
    .single()
    .execSync(clientPtr);

lost its LEFT JOIN clause. The generated SQL still referenced wallets.amount, causing a database error or incorrect query behavior.

Changes

Copy joins_ in the TransformBuilder<T, SelectAll, true> conversion constructor:

this->joins_ = tb.joins_;

This keeps FROM, JOIN, filters, ordering, limit, and offset unchanged when .single() only changes the result cardinality.

Tests

  • Added model-result regression tests for LEFT JOIN + single().
  • Added Row-result regression tests that select wallets.amount.
  • Added tests to the PostgreSQL, MySQL, and SQLite sections of orm_lib/tests/db_test.cc.
  • SQLite database tests passed: 124 assertions in 3 test cases.
  • clang-format and git diff --check passed.

Fixes #2577

@an-tao
an-tao merged commit eba59fe into drogonframework:master Sep 3, 2026
34 checks passed
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.

QueryBuilder::single() drops JOIN clauses

2 participants