【发布时间】:2020-12-16 12:49:05
【问题描述】:
谁能告诉我为什么我的外键格式不正确?
下面是部分代码:
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->onDelete('cascade');
$table->string('title');
$table->text('content');
$table->string('featured');
$table->string('slug');
$table->foreignId('category_id')->constrained()->onDelete('cascade');
$table->softDeletes();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
这是分类表:
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
注意
它不会仅针对类别删除“user_id”外键的此错误。
解决此问题
只需更改数据库/迁移的日期 - 例如,如果您希望在帖子表执行此更改之前创建类别,则迁移:
2020_08_06_172006_create_posts_table.php 2020_08_09_172006_create_categories_table.php
更改类别迁移的日期 2020_08_09 到例如 2020_07_09
【问题讨论】:
标签: laravel eloquent-relationship