SQLSTATE[42S22]:列未找到:1054在'where clause'中未知的列''

6 浏览
0 Comments

SQLSTATE[42S22]:列未找到:1054在'where clause'中未知的列''

我一直遇到以下错误:\n

\n\"SQLSTATE[42S22]: Column not found: 1054 Unknown column \'\' in \'where clause\' \"\n

\nSQL输出如下:\n

    update `contactables` set `updated_at` = 2019-08-16 20:35:56, `active` = 0 where `` is null and `key_ID` = 235852

\n我试图在多态模型上使用精简的firstOrNew/firstOrCreate方法。我尝试了各种变体,如下所示:\n

  $record1 = Contactable::firstOrNew(
    [
     'contactable_ID' => $location_ID,
     'user_ID' => $user_ID,
     'contactable_type' => Location::class,
    ]);
  $record = Contactable::find($record1->key_ID);
  $record->fill(
    [
     'active' => $active,
    ]);
  $record->save();

\n

    $primaryKey = 'key_ID';
    $guarded = [];
    $fillable = [
        'user_ID',
        'use_loc_contact_info',
        'contactable_ID',
        'contactable_type',
        'active',
        'flag',
        'print_and_save',
        'marketing',
      ];

\n只有在我真正更改记录时才出现此问题。创建记录似乎运行正常,在不更改任何内容的情况下更新也似乎正常。我完全不知道接下来该怎么办...\n我查看了以下链接(以及其他十多个与此问题无关的链接):\nLaravel eloquent firstOrNew method doesn\'t update or insert\nlaravel auth:api returns SQLSTATE[42S22]: Column not found: 1054 Unknown column \'\' in \'where clause\' (SQL: select * from `users` where ``\n感谢任何帮助或新问题!

0
0 Comments

问题原因:出现这个错误是因为在SQL查询中使用了一个未知的列名''。

解决方法:根据Laravel框架的版本,可以在相应的模型中插入以下代码来解决这个问题。需要将代码中的'column1'和'column2'替换为实际表中的复合主键列名。

/**
 * Set the keys for a save update query.
 * This is a fix for tables with composite keys
 * TODO: Investigate this later on
 *
 *   \Illuminate\Database\Eloquent\Builder  $query
 *  \Illuminate\Database\Eloquent\Builder
 */
protected function setKeysForSaveQuery(Builder $query)
{
    $query
        //Put appropriate values for your keys here:
        ->where('column1', '=', $this->column1)
        ->where('column2', '=', $this->column2);
    return $query;
}

此外,还需要在代码中引入以下命名空间:

use \Illuminate\Database\Eloquent\Builder;

如果这个问题和解决方法对你有帮助,请考虑为解决方法投票。感谢你的贡献!

0
0 Comments

问题:SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where clause'的出现原因和解决方法

在查找了一些资料后,我终于找到了一个解决方案,该解决方案是基于我偶然发现的这个问题。我忘记了这个表具有一个复合键,这就是问题的根源。

我在我的自定义中包含了以下内容,以覆盖laravel默认的"setKeysForSaveQuery":

class Contactable extends MorphPivot {

...

protected function setKeysForSaveQuery(Builder $query) {

$query

//在这里放入适当的键值:

->where('contactable_type', '=', $this->contactable_type)

->where('contactable_ID', '=', $this->contactable_ID)

->where('user_ID', '=', $this->user_ID);

return $query;

}

...

}

它完美地解决了问题,并修复了eloquent创建的...where '' is null and 'key_ID' = 235852查询的问题。

我还参考了以下内容,它们对我的解决方案有所帮助,但大多数并没有解决我过于复杂的自定义多态关系中的复合主键问题。我希望这条路径能帮助其他人。

Laravel eloquent firstOrNew方法不更新或插入

创建和更新Laravel Eloquent

https://github.com/laravel/framework/issues/2393

https://github.com/laravel/framework/issues/5355

Laravel模型具有两个主键的更新

https://laravel.com/docs/5.7/migrations#creating-indexes

如何在Laravel 5中在模型中使用复合键?

https://blog.maqe.com/solved-eloquent-doesnt-support-composite-primary-keys-62b740120f

https://www.reddit.com/r/laravel/comments/alrgwk/composite_key_in_table_only/

0