Laravel 5. Composite primary key. Unknown column 'id' in 'where clause'

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();

Then I have the error:

Unknown column 'id' in 'where clause'.

The migration file looks like this:

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

Upvotes: 0

Views: 1491

Answers (1)

Jonas Staudenmeir
Jonas Staudenmeir

Reputation: 25906

Laravel doesn't support composite primary keys.

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

Upvotes: 2

Related Questions