我在DB中不使用'id‘列。
相反,我使用复合主键user_id + tmdb_id。
如果我添加这样的新记录:
$movie = new Movie();
$movie->user_id = 1;
$movie->tmdb_id = 2;
$movie->ratio = 3;
$movie->save();很好用!
但是,如果我试图编辑这样的现有记录:
$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']);
});
}发布于 2018-04-19 22:27:12
Laravel不支持复合主键。
您必须使用像https://github.com/mpociot/laravel-composite-key这样的附加包。
https://stackoverflow.com/questions/49924862
复制相似问题