Laravel 5.复合主键. "where子句"中的未知列"id"

问题描述:

我不在数据库中使用"id"列.
相反,我使用复合主键 user_id + tmdb_id .
如果我这样添加新记录:

I don't use 'id' column in DB.
Instead, I use a composite primary key user_id + tmdb_id.
If I add new record like this:

$movie = new Movie();
$movie->user_id = 1;
$movie->tmdb_id = 2;
$movie->ratio = 3;
$movie->save();

一切正常!
但是,如果我尝试编辑这样的现有记录:

it works fine!
But if I try to edit an existing record like this:

$movie = Movie::where([
            'user_id' => 1,
            'tmdb_id' => 2,
        ])->first();

$movie->ratio = 4;
$movie->save();

然后我出现错误:

Unknown column 'id' in 'where clause'.

迁移文件如下:

public function up()
{
    Schema::create('movies', function (Blueprint $table) {

        $table->integer('user_id')->unsigned();
        $table->integer('tmdb_id')->unsigned();
        $table->tinyInteger('ratio');

        // composite primary key
        $table->primary(['user_id', 'tmdb_id']);
    });
}

Laravel不支持复合主键.

Laravel doesn't support composite primary keys.

您必须使用其他软件包,例如> https://github.com/mpociot/laravel -复合键.

You have to use an additional package like https://github.com/mpociot/laravel-composite-key.