Integrity constraint violation: 1052 Column 'status' in where

时间:2021-08-13

thinkphp或fastadmin查询时报错:SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'status' in where clause is ambiguous

模型中关联了其它表

public function testuser()
{
     return $this->belongsTo(Admin::class, 'test_admin_id', 'id', [], 'left')->setEagerlyType(0);
}

解决办法

控制器查询方法里,查询条件中标明是哪个表的字段!

$list = $this->model->where($where)
    ->where([
        'machine.type' => 'own',
        'machine.status' => 'test'
    ])
    ->with(['testuser' => function ($query) {
        $query->withField('username');
     }])
    ->order($sort, $order)
    ->paginate($limit);

withField 可以避免关联表中同主表中字段一样的数据覆盖问题,同时也可以查询关联表中指定的字段。