问题
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']);
});
}
回答1:
Laravel doesn't support composite primary keys.
You have to use an additional package like https://github.com/mpociot/laravel-composite-key.
回答2:
Go to your Movie.php
model and add this
protected $primaryKey = ['user_id', 'tmdb_id'];
The primary key is normally assumed to be id
.
来源:https://stackoverflow.com/questions/49924862/laravel-5-composite-primary-key-unknown-column-id-in-where-clause