Skip to content

Conversation

@pranavbabu
Copy link

Problem

When searching cards with multiple terms, SQLite raised an ambiguous column error:

SQLite3::SQLException: ambiguous column name: search_records_fts

This occurred because each term in the search was calling .mentioning() separately in a reduce loop:

result = terms.reduce(result) do |result, term|
  result.mentioning(term, user: creator)
end

Each .mentioning() call added another INNER JOIN to the search_records_fts table, creating duplicate joins with the same table name, which SQLite couldn't disambiguate.

Solution

Combined all search terms into a single FTS query using the AND operator before calling .mentioning() once:

result = result.mentioning(combined_fts_query, user: creator) if terms.present?

This ensures only one join to search_records_fts is created, regardless of the number of search terms.

Also added FTS sanitization by escaping double quotes to prevent query syntax errors when users search for terms containing special characters like O"Reilly or user's input.

Copy link
Member

@jorgemanrubia jorgemanrubia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @pranavbabu!

end

def combined_fts_query
terms.map { |term| sanitize_fts_term(term) }.join(' AND ')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will the AND and the sanitization logic work for MySQL FTS too?

cc @djmb in case he has insight here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to what AI said, it will not work in MySQL FTS and should be handled seprately

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