Column 'xxx' in order clause is ambiguous

We are encountering a SQL error: Unknown column ‘description’ in ‘order clause’. This occurs when we join with other tables.

In our Gii-created search model, we added a join with 2 other models:

$query = Flights::find();

$query->joinWith([‘panel’,‘varietal’,]);

When we run the application, we get this error:

SQLSTATE[23000]: Integrity constraint violation: 1052 Column ‘description’ in order clause is ambiguous

The SQL being executed was: SELECT COUNT(*) FROM flights LEFT JOIN panel ON flights.panel_rec_id = panel.rec_id LEFT JOIN varietals ON flights.varietal_rec_id = varietals.rec_id ORDER BY description

Both the ‘panel’ and the ‘varietal’ tables have a column named description.The base model, Flights, does ‘not’ have column named description.

We tried adding the orderby clause, ->orderBy(‘varietal.description asc’), but this did not resolve the issue.

The model relationships are:

public function getPanel()


{


    return $this->hasOne(Panel::className(), ['rec_id' => 'panel_rec_id']);


}





public function getVarietal()


{


    return $this->hasOne(Varietals::className(), ['rec_id' => 'varietal_rec_id']);


}

What do we need to do to resolve the ‘ambiguous’ error?

It’s indeed not clear what you’ve meant there. Adding flights.description (or whatever table it is) should solve it.

If not, introduce table alias and use it.