Table-qualify amount/expires_at scopes so they compose with joins#5
Open
rameerez wants to merge 1 commit into
Open
Table-qualify amount/expires_at scopes so they compose with joins#5rameerez wants to merge 1 commit into
rameerez wants to merge 1 commit into
Conversation
`credits`/`debits`/`not_expired`/`expired` used bare `where("amount > 0")`
-style predicates. A bare column name is ambiguous the moment a host joins
another table carrying the same column — and every wallet has an `amount`
on BOTH wallets_transactions and wallets_transfers, so a natural query like
wallet.transactions.joins(:transfer).credits
(find the transactions belonging to a category of transfer — e.g. reversals)
raised PG::AmbiguousColumn; SQLite and MySQL raise the equivalent.
Route the predicates through `arel_table[:col]` so the generated SQL is
always table-qualified. This also respects the embedding `table_name`
override for free (arel_table derives from the resolved table name), so
embedded/subclassed transaction tables qualify correctly too.
Regression test reproduces the ambiguity across the transfer join and
asserts the generated SQL is qualified (DB-agnostic).
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.
Problem
credits/debits(andnot_expired/expired) use bare column predicates:A bare
amountis ambiguous the moment a host joins another table that also has anamountcolumn — and every wallet has anamounton bothwallets_transactionsandwallets_transfers. So a perfectly natural query like:raises
PG::AmbiguousColumn(SQLite and MySQL raise the equivalent —ambiguous column name: amount).We hit this in production (a real-money carpooling app): computing "lifetime earned, net of reversals" needs exactly that join, and we had to work around it host-side with a two-step id resolution instead of a clean
.joins(:transfer).credits.Fix
Route the four column-referencing raw-SQL scopes through
arel_table[:col], so the generated SQL is always table-qualified ("wallets_transactions"."amount" > 0). Bonus:arel_tablederives from the resolved (overridable)table_name, so the embedding/subclassing story qualifies correctly for free — no hardcoded table string.Test
New regression in
transaction_test.rb:wallets_transfers.amountexist),wallet.transactions.joins(:transfer).debits/.credits— which raised before the fix (verified: reverting the scopes reproducesSQLite3::SQLException: ambiguous column name: amount),Full suite green (93 runs, 346 assertions).