Laravel 5.复合主键. "where子句"中的未知列"id" [英] Laravel 5. Composite primary key. Unknown column 'id' in 'where clause'
本文介绍了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.
这篇关于Laravel 5.复合主键. "where子句"中的未知列"id"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文