简体   繁体   中英

Yii2 GridView: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in order clause is ambiguous

could someone explain me why I'm getting this error? I've tried to search related information, but couldn't find what would be useful.

Here is my function:

$query = Recipient::find()->from('recipient')
            ->joinWith(['delivers'])
     
    $dataProvider = new ActiveDataProvider([
        'query' => $query,

    ]);

Error is:

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in order clause is ambiguous

The SQL being executed was:

SELECT COUNT(*) FROM `recipient` 
LEFT JOIN `deliver` ON `recipient`.`id` = `deliver`.`recipient_id` 
ORDER BY `id` DESC

Try

Recipient::find()->from('recipient')->joinWith(['delivers'])->orderBy(['`recipient`.`id`' => SORT_ASC]);

In above example, column name is explicitly specified to avoid ambiguity error.

Code seems incomplete. Post more code, so we can help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM