Skip to content

Conversation

@nurikk
Copy link

@nurikk nurikk commented Dec 30, 2025

fix ambiguous column error when searching or sorting by multiple fields from same relationship

Fixes SQLite 'ambiguous column name' error that occurred when users configured column_searchable_list or column_sortable_list with multiple fields from the same related model (e.g., ['user.id', 'user.name']).

The issue happened because:

  • Each field was independently joining the same table multiple times
  • sort_query runs before search_query, so duplicate joins could occur across methods
  • SQLite cannot resolve ambiguous column references with duplicate table joins

Solution:

  • Added _get_joined_entities() to inspect existing joins in the SQL statement
  • Added _join_relationship_paths() as DRY helper to track and prevent duplicate joins
  • Refactored both search_query and sort_query to use the helper
  • Joins are now tracked both by relationship path (within method) and by inspecting the statement (cross-method), ensuring no duplicates

Users can now safely use multiple fields from the same relationship in searchable and sortable columns without encountering database errors.

…e fields from same relationship

Fixes SQLite 'ambiguous column name' error that occurred when users configured
column_searchable_list or column_sortable_list with multiple fields from the
same related model (e.g., ['user.id', 'user.name']).

The issue happened because:
- Each field was independently joining the same table multiple times
- sort_query runs before search_query, so duplicate joins could occur across methods
- SQLite cannot resolve ambiguous column references with duplicate table joins

Solution:
- Added _get_joined_entities() to inspect existing joins in the SQL statement
- Added _join_relationship_paths() as DRY helper to track and prevent duplicate joins
- Refactored both search_query and sort_query to use the helper
- Joins are now tracked both by relationship path (within method) and by inspecting
  the statement (cross-method), ensuring no duplicates

Users can now safely use multiple fields from the same relationship in searchable
and sortable columns without encountering database errors.
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