未找到Laravel Eloquent更新列:1054未知列';id';在';其中条款';


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

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

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

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

$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');
            }

关系

用户型号

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');
    }

}

元数据模型

class Metadata extends Eloquent {
    public $timestamps = false;
    protected $table = "users_metadata";
    public function user()
    {
        return $this->belongsTo('user');
    }
}

真正奇怪的是,如果我在验证中留下$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}}

如果我把它评论出来,一切都很好,工作得很好,检查了所有的东西,我没有看到任何奇怪的东西。

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

听起来user_metadata表不包含users表的外键,这是连接用户和他/她的元数据所必需的。看起来该列也应该命名为id

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

假设users表中有一个自动递增的id列,那么user_metadata迁移文件可能包含以下内容:

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

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

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

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

我只是想添加到这个问题中,因为我遇到了这个问题,找不到解决方案。

updateOrCreate将尝试查找具有默认id列的记录。但是,如果您的表有一个不同的主键列而不是默认的主键列,那么您必须在模型文件中定义主键列,如下所示:

protected $primaryKey = '<your column name>'