Skip to content

Qualify updated_at in document filters to avoid ambiguous column#260

Merged
vswamidass-sfdc merged 1 commit into
mainfrom
fix-ambiguous-updated-at
Jul 7, 2026
Merged

Qualify updated_at in document filters to avoid ambiguous column#260
vswamidass-sfdc merged 1 commit into
mainfrom
fix-ambiguous-updated-at

Conversation

@vswamidass-sfdc

Copy link
Copy Markdown
Collaborator

Problem

The documents index (BaseDocumentsController#index) 500s with PG::AmbiguousColumn: column reference "updated_at" is ambiguous when a since/until date filter is combined with a similarity search whose results are materialized.

The index eager-loads :library and :user. When those associations get forced into the query as LEFT OUTER JOINs (e.g. when similarity results are materialized via pluck), the bare where('updated_at > ?') filter becomes ambiguous — documents, libraries, and users all have an updated_at column.

Fix

Qualify the column as documents.updated_at in both the since and until branches.

Tests

Added test/models/document_similarity_filter_test.rb, which reproduces the exact failing query shape (eager-loaded associations + updated_at filter + materialization):

  • Confirms the qualified filter runs cleanly.
  • Confirms the unqualified filter raises PG::AmbiguousColumn — so this is a real regression guard, not a no-op.

Incidental

The api_tokens fixture was empty ({}) while the schema requires a non-null user_id, which caused fixtures :all to fail and blocked the entire test suite from loading. Populated it minimally so tests can run.

Note

On main today this bug is latent: the controller's similar_to path is lazy, so includes runs 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.

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.
@vswamidass-sfdc vswamidass-sfdc merged commit 35dbe61 into main Jul 7, 2026
3 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.

2 participants