拉拉维尔更改主密钥不起作用



我正在尝试更改Laravel中模型的primaryKey。primaryKey已经在数据库和模型中设置,但两者都不起作用。

我收到这个错误消息:

SQLSTATE[42S22]:未找到列:1054"where子句"中的未知列"(SQL:从users中选择*,其中"`=123456限制1(

当我在模型中取消设置primaryKey时,我会得到以下错误:

SQLSTATE[42S22]:未找到列:1054"where子句"中的未知列"users.id"(SQL:从users中选择*,其中users.id=123456限制1(

这是完全合法的,因为primaryKey应该在Model中设置为"vid"。

以下是我的模型的重要部分:

<?php
namespace App;
use IlluminateDatabaseEloquentModel;
use IlluminateNotificationsNotifiable;
class User extends Model implements IlluminateContractsAuthAuthenticatable
{
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();
});

我真的不能告诉你出了什么问题,但重新构建用户模型有帮助。

最新更新