phacility/phabricator · error · Exception

Order vector "%s" is invalid: only the last column in an ord

Error message

Order vector "%s" is invalid: only the last column in an order may be unique, but "%s" is a unique column and not the last column in the order.

What it means

Thrown by setOrderVector() when a column marked unique appears anywhere except the last position. The vector is validated left to right and then checked inversely: once ordering is decided by a unique column (e.g. 'id'), any later column can never change the order — 'ORDER BY id, title' is meaningless — so unique columns are only legal as the final tiebreaker.

Source

Thrown at src/infrastructure/query/policy/PhabricatorCursorPagedPolicyAwareQuery.php:1085

    // Make sure that the last column is unique so that this is a strong
    // ordering which can be used for paging.
    $last = last($unique);
    if ($last !== true) {
      throw new Exception(
        pht(
          'Order vector "%s" is invalid: the last column in an order must '.
          'be a column with unique values, but "%s" is not unique.',
          $vector->getAsString(),
          last_key($unique)));
    }

    // Make sure that other columns are not unique; an ordering like "id, name"
    // does not make sense because only "id" can ever have an effect.
    array_pop($unique);
    foreach ($unique as $key => $is_unique) {
      if ($is_unique) {
        throw new Exception(
          pht(
            'Order vector "%s" is invalid: only the last column in an order '.
            'may be unique, but "%s" is a unique column and not the last '.
            'column in the order.',
            $vector->getAsString(),
            $key));
      }
    }

    $this->orderVector = $vector;
    return $this;
  }


  /**
   * Get the effective order vector.
   *
   * @return PhabricatorQueryOrderVector Effective vector.

View on GitHub (pinned to 5720a38cfe)

Solutions

  1. Move the unique column to the end: array('title', 'id') instead of array('id', 'title')
  2. Drop the unique column entirely if the remaining last column is itself unique

Example fix

// before: unique column first
$query->setOrderVector(array('id', 'title'));

// after: unique column last
$query->setOrderVector(array('title', 'id'));
Defensive patterns

Strategy: validation

Validate before calling

// Enforce: unique columns may only occupy the last position.
$orderable = $query->getOrderableColumns();
$vector = array_values($vector);
foreach ($vector as $i => $key) {
  $is_unique = idx($orderable[$key], 'unique', false);
  if ($is_unique && $i !== count($vector) - 1) {
    unset($vector[$i]); // drop meaningless components after a unique key
  }
}
$query->setOrderVector(array_values($vector));

Type guard

function vectorHasUniqueOnlyLast(PhabricatorCursorPagedPolicyAwareQuery $query, array $vector) {
  $orderable = $query->getOrderableColumns();
  $last = count($vector) - 1;
  foreach ($vector as $i => $key) {
    if (idx($orderable[$key], 'unique', false) && $i !== $last) { return false; }
  }
  return true;
}

Prevention

When it happens

Trigger: setOrderVector(array('id', 'title')) — id is unique but not last; putting PHID or any unique-key column before another sort column.

Common situations: Developers adding id 'for good measure' at the front of an order; refactors that reorder vector components without noticing id was the terminator.

Related errors


AI-assisted analysis of phacility/phabricator@5720a38cfe (2026-08-21). Data as JSON: /api/errors/48f0d8240fd949c4. Report an issue: GitHub.