Qualify updated_at in document filters to avoid ambiguous column#260
Merged
Conversation
The documents index eager-loads :library and :user. When those associations are forced into the query as JOINs (e.g. when similarity results are materialized) and a since/until date filter references updated_at, Postgres raises PG::AmbiguousColumn because documents, libraries, and users all have an updated_at column. Qualify the filter as documents.updated_at in both the since and until branches. Add a regression test covering the materialized JOIN query shape, and populate the empty api_tokens fixture that was blocking the test suite from loading.
chester-vanhuang
approved these changes
Jul 7, 2026
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
The documents index (
BaseDocumentsController#index) 500s withPG::AmbiguousColumn: column reference "updated_at" is ambiguouswhen asince/untildate filter is combined with a similarity search whose results are materialized.The index eager-loads
:libraryand:user. When those associations get forced into the query asLEFT OUTER JOINs (e.g. when similarity results are materialized viapluck), the barewhere('updated_at > ?')filter becomes ambiguous —documents,libraries, andusersall have anupdated_atcolumn.Fix
Qualify the column as
documents.updated_atin both thesinceanduntilbranches.Tests
Added
test/models/document_similarity_filter_test.rb, which reproduces the exact failing query shape (eager-loaded associations +updated_atfilter + materialization):PG::AmbiguousColumn— so this is a real regression guard, not a no-op.Incidental
The
api_tokensfixture was empty ({}) while the schema requires a non-nulluser_id, which causedfixtures :allto fail and blocked the entire test suite from loading. Populated it minimally so tests can run.Note
On
maintoday this bug is latent: the controller'ssimilar_topath is lazy, soincludesruns as separate preload queries with no JOIN, and the ambiguity isn't reachable through a normal request. The JOIN (and the crash) appears once similarity results are materialized. The qualified-column fix is correct regardless and prevents the latent bug from surfacing.