【问题标题】:Laravel changing of primaryKey don't workLaravel更改primaryKey不起作用
【发布时间】:2020-07-22 01:39:12
【问题描述】:

我正在尝试在 Laravel 中更改我的模型的主键。 primaryKey 已在 Database 和 Model 中设置,但两者都不起作用。

我收到此错误消息:

SQLSTATE[42S22]:找不到列:1054 'where 子句'中的未知列''(SQL:select * from users where ``= 123456 限制 1)

当我在模型中取消设置 primaryKey 时,我收到此错误:

SQLSTATE[42S22]:未找到列:1054 'where 子句'中的未知列 'users.id'(SQL:select * from users where users.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 仍然是同样的错误,即使删除它
  • 你能包括你的用户表迁移吗?

标签: php mysql laravel


【解决方案1】:

我真的无法告诉你出了什么问题,但是重新制作用户模型会有所帮助。

【讨论】:

    猜你喜欢
    • 2017-07-20
    • 1970-01-01
    • 2014-11-29
    • 2020-12-28
    • 1970-01-01
    • 2016-06-30
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多