简体   繁体   English

Laravel Eloquent SQLSTATE[23000]:违反完整性约束:1052 列...在 where 子句中不明确

[英]Laravel Eloquent SQLSTATE[23000]: Integrity constraint violation: 1052 Column ... in where clause is ambiguous

I am getting this error "Integrity constraint violation: 1052 Column 'disabled_at' in where clause is ambiguous" although in my where I added the table name to precede the column, and also in the join statement, but it still gives me the above error.我收到此错误“Integrity constraint violation: 1052 Column 'disabled_at' in where clause is ambiguous”虽然在我的位置我添加了表名在列之前,也在连接语句中,但它仍然给我上述错误. Here is that code snipet:这是代码片段:

        $name = ($params['query']) ? $params['query'] : '';
        $perPage = (isset($params['perpage']) && $params['perpage'] !== null) ? 
            intval($params['perpage']) : 50;

        $query = $this->with('sections');
              
        // search disabled users
        if ($disabled) {
            $query->withTrashed();
            // if not all, limit to disabled only
            if (!$allUsers) {
                $query->whereNotNull('users.disabled_at');
            }
        }

        // exclude name search when blank to increase query speed
        if ($name !== '') { 
            $query->join('sections', 'users.section_id', '=', 'sections.id');
            $query->where(DB::raw("CONCAT(users.title,' ',users.first_name,' ',users.last_name,' ',users.email,' ',sections.name)"), 'like', '%' . $name . '%');
        }

Any help will be appreciated.任何帮助将不胜感激。

well, thank all you for your suggestions.嗯,谢谢大家的建议。 I figured out what the issue was by seeing the raw mysql query using your suggestions, outputting with DB::enableQueryLog() and query->toSQL() , the later actually is what worked for me.通过使用您的建议查看原始 mysql 查询,使用DB::enableQueryLog()query->toSQL()输出,我发现了问题所在,后者实际上对我有用。 Basically the column disabled_at works as the soft delete 'deleted_at' , so it's either NULL or has a timestamp, therefore what was happening was that the raw query had a "...where '.disabled_at' IS NULL..." by default (unbeknown to me), and because the sections table also has a 'disabled_at' column, it did not know to which of those 2 tables disabled_at belonged, so I had to specify the table with the following query" whereNULL('users.disabled_at') ..." and not just let the default run.基本上列disabled_at用作软删除'deleted_at' ,所以它要么是 NULL 要么有时间戳,因此发生的事情是原始查询默认有一个“... where '.disabled_at' IS NULL ...” (我不知道),并且因为 sections 表也有一个 'disabled_at' 列,它不知道disabled_at属于这 2 个表中的哪一个,所以我不得不使用以下查询指定表" whereNULL('users.disabled_at') ..." 而不仅仅是让默认运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel 6 错误:SQLSTATE[23000]:违反完整性约束:1052 where 子句中的列“id_perusahaan”不明确 - Laravel 6 Error : SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id_perusahaan' in where clause is ambiguous SQLSTATE [23000]:违反完整性约束:1052 where 子句中的列“值”不明确 - SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'value' in where clause is ambiguous SQLSTATE [23000]:完整性约束违规:1052 order order中的'created_at'列不明确Laravel 5.5 - SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in order clause is ambiguous Laravel 5.5 SQLSTATE [23000]:完整性约束违规:1052 列“created_at”在 order 子句中不明确 Laravel 8 - SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in order clause is ambiguous Laravel 8 SQLSTATE [23000]:完整性约束违规:1052 where子句中的列'status'不明确 - SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'status' in where clause is ambiguous SQLSTATE [23000]:完整性约束违规:1052 where子句中的列'NRP'不明确 - SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'NRP' in where clause is ambiguous Yii2 GridView:SQLSTATE[23000]:违反完整性约束:1052 order 子句中的列“id”不明确 - Yii2 GridView: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in order clause is ambiguous Laravel 完整性约束违规:1052 列 'id' in where 子句不明确 - Laravel Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous Laravel Eloquent:违反完整性约束:where 子句中的 1052 列“id”不明确 - Laravel Eloquent: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous 完整性约束违规:1052 where子句中的列'id'不明确 - Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM