【问题标题】:Laravel 5. Composite primary key. Unknown column 'id' in 'where clause'Laravel 5. 复合主键。 'where 子句'中的未知列'id'
【发布时间】:2018-04-19 15:29:03
【问题描述】:

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

【问题讨论】:

    标签: database laravel-5 laravel-eloquent composite-primary-key


    【解决方案1】:

    Laravel 不支持复合主键。

    你必须使用一个额外的包,比如https://github.com/mpociot/laravel-composite-key

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-20
      • 2021-06-12
      • 2015-06-03
      • 2019-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多