【发布时间】:2022-01-25 20:14:21
【问题描述】:
我在 Laravel 8 中有一个数据库迁移,如下所示:
class CreateArticlesTable extends Migration
{
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->id();
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->string('title');
$table->string('slug');
$table->text('description');
$table->text('body');
$table->string('imageUrl');
$table->string('tags');
$table->integer('viewCount')->default(0);
$table->integer('commentCount')->default(0);
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('articles');
}
}
但是每当我想运行它时,我都会收到此错误:
一般错误:1005 无法创建表elearning.articles(errno:150“外键约束格式不正确”)
我不知道这里到底出了什么问题,所以如果你知道如何解决这个问题,请告诉我...
【问题讨论】:
-
@rickdenhaan 不,它不能回答我的问题,因为该问题的解决方案不能应用于这个问题。
-
那么您能否编辑您的问题并解释您尝试了哪些解决方案以及为什么它们不适合您的情况?我链接的问题有 27 个答案以及针对此问题的各种可能的解决方案,从检查以确保表是 InnoDB 表到确保以正确的顺序创建表并且您链接在一起的两列是相同的类型和属性。
标签: php mysql laravel laravel-8 laravel-migrations