Laravel lumen另一个表id关系



我做错了什么?

选项迁移文件

Schema::create('option', function (Blueprint $table) {
      $table->increments('id');
      $table->integer('contents_content_id')->unsigned();
      $table->timestamps();
      $table->foreign('contents_content_id')
      ->references('content_id')
      ->on('contents');
});

contents-migration-file

 Schema::create('contents', function (Blueprint $table) {
          $table->increments('id');
          $table->integer('content_id');
          $table->timestamps();
   });
误差

[照亮数据库 QueryException]SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table option add constraint option_content_content_id_foreign foreign key (content_content_id) references contents (content_id))

[PDOException]SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

问题是option表在contents表之前被创建,当你在content上引用content_id时,content表还不存在。

注意:

每个迁移文件名包含一个时间戳,允许Laravel确定迁移的顺序。

所以你现在要做的,是从database/migrations文件夹中删除迁移并按顺序重新创建它们:首先是options,然后是contents

最新更新