【发布时间】:2020-07-22 01:39:12
【问题描述】:
我正在尝试在 Laravel 中更改我的模型的主键。 primaryKey 已在 Database 和 Model 中设置,但两者都不起作用。
我收到此错误消息:
SQLSTATE[42S22]:找不到列:1054 'where 子句'中的未知列''(SQL:select * from
userswhere ``= 123456 限制 1)
当我在模型中取消设置 primaryKey 时,我收到此错误:
SQLSTATE[42S22]:未找到列:1054 'where 子句'中的未知列 'users.id'(SQL:select * from
userswhereusers.id= 123456 限制 1)
这是完全合法的,因为模型中的 primaryKey 应该设置为 'vid'。
这是我的模型的基本部分:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
class User extends Model implements \Illuminate\Contracts\Auth\Authenticatable
{
use Notifiable;
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'vid' => 'string'
];
protected $table = 'users';
protected $primaryKey = 'vid';
public $incrementing = false;
}
迁移:
Schema::create('users', function (Blueprint $table) {
$table->string('vid');
$table->string('name');
$table->string('surname');
$table->integer('val1');
$table->integer('val2');
$table->integer('val3');
$table->string('val4');
$table->string('val5');
$table->integer('val6');
$table->integer('val7');
$table->integer('val8');
$table->dateTime('val9');
$table->string('val10');
$table->string('val11');
$table->timestamps();
});
【问题讨论】:
-
尝试将
protected $keyType = 'string';添加到您的用户模型中。 -
@DigitalDrifter 没用,同样的错误
SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where clause' (SQL: select * from users where `` = 123456 limit 1) -
也许尝试删除 casts 属性。它可能在某个地方发生冲突,但并不完全确定。
-
@DigitalDrifter 仍然是同样的错误,即使删除它
-
你能包括你的用户表迁移吗?