Laravel lumen 另一个 table id 关系
Laravel lumen another table id relationship
我做错了什么?
选项-迁移文件
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');
});
内容迁移文件
Schema::create('contents', function (Blueprint $table) {
$table->increments('id');
$table->integer('content_id');
$table->timestamps();
});
错误
[Illuminate\Database\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
table 在 contents
table 之前创建,当您在 content
上引用 content_id
时, content
table 还不存在。
注意:
Each migration file name contains a timestamp which allows Laravel to determine the order of the migrations.
所以您现在要做的是从 database/migrations
文件夹中删除迁移并按顺序重新创建它们:首先 options
然后 contents
.
我做错了什么?
选项-迁移文件
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');
});
内容迁移文件
Schema::create('contents', function (Blueprint $table) {
$table->increments('id');
$table->integer('content_id');
$table->timestamps();
});
错误
[Illuminate\Database\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
table 在 contents
table 之前创建,当您在 content
上引用 content_id
时, content
table 还不存在。
注意:
Each migration file name contains a timestamp which allows Laravel to determine the order of the migrations.
所以您现在要做的是从 database/migrations
文件夹中删除迁移并按顺序重新创建它们:首先 options
然后 contents
.