jeudi 19 avril 2018

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



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2Hfb5HO
via IFTTT

Aucun commentaire:

Enregistrer un commentaire