mattermost-community/focalboard · error

SearchBoardsForUser error getting unionSQL: %w

Error message

SearchBoardsForUser error getting unionSQL: %w

What it means

The final UNION query (boardMembersQ unioned with teamMembersSQL) failed ToSql() in SearchBoardsForUser. The combined prefix/suffix construction produced an invalid query that squirrel could not render.

Source

Thrown at server/services/store/mattermostauthlayer/mattermostauthlayer.go:774

	}
	// NOTE: theoretically, could do e.g. `isGuest := !includePublicBoards`
	// but that introduces some tight coupling + fragility
	if !user.IsGuest {
		unionQ = unionQ.
			Prefix("(").
			Suffix(") UNION ("+channelMembersSQL+")", channelMembersArgs...)
		if includePublicBoards {
			unionQ = unionQ.Suffix(" UNION ("+teamMembersSQL+")", teamMembersArgs...)
		}
	} else if includePublicBoards {
		unionQ = unionQ.
			Prefix("(").
			Suffix(") UNION ("+teamMembersSQL+")", teamMembersArgs...)
	}

	unionSQL, unionArgs, err := unionQ.ToSql()
	if err != nil {
		return nil, fmt.Errorf("SearchBoardsForUser error getting unionSQL: %w", err)
	}

	// if we're using postgres or sqlite, we need to replace the
	// question mark placeholder with the numbered dollar one, now
	// that the full query is built
	if s.dbType == model.PostgresDBType || s.dbType == model.SqliteDBType {
		var rErr error
		unionSQL, rErr = sq.Dollar.ReplacePlaceholders(unionSQL)
		if rErr != nil {
			return nil, fmt.Errorf("SearchBoardsForUser unable to replace unionSQL placeholders: %w", rErr)
		}
	}

	rows, err := s.mmDB.Query(unionSQL, unionArgs...)
	if err != nil {
		s.logger.Error(`searchBoardsForUser ERROR`, mlog.Err(err))
		return nil, err
	}

View on GitHub (pinned to a84bbb65e3)

Solutions

  1. Fix the root ToSql error in boardMembersQ (or the earlier builders) first — the union inherits builder state.
  2. Validate the Suffix SQL text and that args counts match '?' placeholders.
  3. Test the union construction directly with a fixed set of inputs.
  4. Check squirrel version for Prefix/Suffix with args regressions.
Defensive patterns

Strategy: try-catch

Try / catch

boards, err := store.SearchBoardsForUser(userID, term)
if err != nil {
	if strings.Contains(err.Error(), "error getting unionSQL") {
		logger.Error("union query build failed in board search", "err", err)
		return nil, ErrInternalSearch
	}
	return nil, err
}

Prevention

When it happens

Trigger: unionQ.ToSql() fails after union of boardMembersQ with the already-rendered teamMembersSQL via Prefix/Suffix — e.g. boardMembersQ itself was invalid, so combining propagates the failure.

Common situations: Same root causes as the earlier ToSql failures cascading into the union; edit mistakes in the Suffix SQL strings breaking placeholder/arg pairing.

Related errors


AI-assisted analysis of mattermost-community/focalboard@a84bbb65e3 (2026-08-30). Data as JSON: /api/errors/7875a51d31611ece. Report an issue: GitHub.