未找到 Laravel Eloquent 更新列:“where 子句”中的 1054 列“id”未知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18120056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:14:38  来源:igfitidea点击:

Laravel Eloquent Update Column not found: 1054 Unknown column 'id' in 'where clause'

phpsql-updatelaravel

提问by Torkil Johnsen

I never had any problems with Laravel but i ran into something what is really strange.

我在 Laravel 上从来没有遇到过任何问题,但我遇到了一些非常奇怪的事情。

I have a one to onerelation storing users metadata in a seperate table.

我有一个one to one将用户元数据存储在单独表中的关系。

my Controller update looks like this

我的控制器更新看起来像这样

$val = Validator::make(Input::all(), $rules);

        if($val->passes())
        {
            $this->cur_user->username             = Input::get('username');
            $this->cur_user->metadata->first_name = Input::get('first_name');
            $this->cur_user->metadata->last_name  = Input::get('last_name');
            $this->cur_user->metadata->gender     = Input::get('gender');
            $this->cur_user->metadata->location   = Input::get('location');
            $this->cur_user->email                = Input::get('email');
            $this->cur_user->metadata->dob        = Input::get('dob');
            $this->cur_user->metadata->about      = Input::get('about');

            if ($this->cur_user->save() && $this->cur_user->metadata->save()) 
            {
               $data = array('msg' => 'success');
            }

Relations

关系

Users Model

用户模型

class User extends Eloquent implements UserInterface, RemindableInterface {

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password');

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->password;
    }

    /**
     * Get the e-mail address where password reminders are sent.
     *
     * @return string
     */
    public function getReminderEmail()
    {
        return $this->email;
    }


    public function metadata()
    {
        return $this->hasOne('metadata');
    }


}

Metadata Model

元数据模型

class Metadata extends Eloquent {

    public $timestamps = false;

    protected $table = "users_metadata";

    public function user()
    {
        return $this->belongsTo('user');
    }
}

What is really weird, if i leave the $this->cur_user->metadata->gender = Input::get('gender');and $this->cur_user->metadata->about = Input::get('about');in my validation i get the following error

真的很奇怪,如果我离开 $this->cur_user->metadata->gender = Input::get('gender');并且$this->cur_user->metadata->about = Input::get('about');在我的验证中我会收到以下错误

{"error":{"type":"Exception","message":"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: update `users_metadata` set `gender` = ?, `about` = ? where `id` is null) (Bindings: array (\n  0 => '1',\n  1 => 'testing one 2 3',\n))","file":"C:\BitNami\apache2\htdocs\laravel\vendor\laravel\framework\src\Illuminate\Database\Connection.php","line":555}}

If i comment it out, all fine and works well, checked everything i do not see any strange things.

如果我将其注释掉,一切都很好并且运行良好,检查了所有我没有看到任何奇怪的事情。

Could please someone give me a hint? (searched around found some answers but not really for my problem)

可以请有人给我一个提示吗?(四处搜索找到了一些答案,但并不是针对我的问题)

回答by Torkil Johnsen

Sounds like your user_metadatatable does not contain a foreign key to the userstable, which it is requiring to be able to connect a user and his/her metadata. It also looks like the column is expected to be named id.

听起来你的user_metadata表不包含表的外键users,它需要能够连接用户和他/她的元数据。看起来该列也应该被命名为id

It would be helpful if you could post your database structure, or even better: Your migration files. I could venture a suggestion though:

如果您可以发布您的数据库结构,或者甚至更好:您的迁移文件,那将会很有帮助。不过我可以大胆提出一个建议:

Provided that you have an auto incrementing idcolumn in your userstable, your user_metadatamigration file could contain something like this:

如果id您的users表中有一个自动递增列,您的user_metadata迁移文件可能包含如下内容:

$table->integer('user_id')->unsigned();
$table->primary('user_id');
$table->foreign('user_id')
    ->references('id')->on('users')
    ->onDelete('cascade');

Perhaps you are missing some parts in your setup?

也许您的设置中缺少某些部分?

In other words, user_metadata.user_idshould be an identifying foreign key, reflecting users.id. And when a user is deleted, his/her metadata should automatically be deleted too (unless you're using for instance MyISAM, which that does not support foreign key relations).

换句话说,user_metadata.user_id应该是一个识别外键,反映users.id. 并且当用户被删除时,他/她的元数据也应该自动删除(除非您使用例如 MyISAM,它不支持外键关系)。

See also the manual for more info on Laravel/Eloquent foreign keys: http://four.laravel.com/docs/schema#foreign-keys

有关 Laravel/Eloquent 外键的更多信息,另请参阅手册:http://four.laravel.com/docs/schema#foreign-keys