我做错了什么?
选项迁移文件
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
) referencescontents
(content_id
))[PDOException]SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
问题是option
表在contents
表之前被创建,当你在content
上引用content_id
时,content
表还不存在。
注意:
每个迁移文件名包含一个时间戳,允许Laravel确定迁移的顺序。
所以你现在要做的,是从database/migrations
文件夹中删除迁移并按顺序重新创建它们:首先是options
,然后是contents
。