drupal-7.56\includes\database\select.inc has function

  public function orderBy($field, $direction = 'ASC') {
    // Only allow ASC and DESC, default to ASC.
    $direction = strtoupper($direction) == 'DESC' ? 'DESC' : 'ASC';
    $this->order[$field] = $direction;
    return $this;
}

so it means, that I can't sort data -> ASC NULLS FIRST OR DESC NULLS LAST, etc.

should be changed to something like this:

 public function orderBy($field, $direction = 'ASC') {
    // Only allow ASC and DESC, default to ASC.
    
    $direction = strtoupper($direction);
    if ($direction !="ASC NULLS FIRST"){
    $direction == 'DESC' ? 'DESC' : 'ASC';
    }
    // 
    $this->order[$field] = $direction;
    return $this;
  }

MySQL exports nulls first, but PostgreSQL export nulls last, so there is big problem if db are switched and there is no way to fix this problem, because there is only ASC and DESC values allowed.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

B.Mafia created an issue. See original summary.

B.Mafia’s picture

Issue summary: View changes
MustangGB’s picture

Priority: Critical » Normal

Seems like a fair request, although not many people are complaining about it, doesn't appear to lead to data loss, and there is a workaround (pick your DB wisely), so not sure this counts as critical.

Liam Morland’s picture

Issue summary: View changes
Issue tags: -postgree +PostgreSQL
pounard’s picture

The best solution would be to have the NULL FIRSTS | LAST generic feature on the query builder, whatever is the SQL engine, then business-specific code would call $select->orderByNullFirst() by themselves whenever necessary. Only SQLite will not support this, but both MySQL and PostgreSQL do.

bzrudi71’s picture

Issue tags: -sort, -db

Here is how we did it for Drupal 8 - guess this could be backported. #2443657: PostgreSQL: Fix system\Tests\Entity\EntityQueryTest

poker10’s picture

Adding a patch to backport D8 issue: #2443657: PostgreSQL: Fix system\Tests\Entity\EntityQueryTest

However this will change the ORDER BY behavior with NULLs in PostgreSQL so possibly it can cause some regressions in contrib modules (if these relies on such order). So it needs to be considered if we want the consistent behavior between MySQL/PostgreSQL and if it is worth the potential troubles (in this D7 stage).