【问题标题】:Why it appears the error "General error: 1005 Can't create table"?为什么会出现错误“General error: 1005 Can't create table”?
【发布时间】:2020-05-22 04:45:26
【问题描述】:

我有这两个迁移文件,一个用于创建 faq_channels 表,另一个用于创建 faq_questions 表。但是,我不明白为什么它会显示错误:

    SQLSTATE[HY000]: General error: 1005 Can't create table `test`.`faq_questions` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `faq_questions` add const
raint `faq_questions_channel_id_foreign` foreign key (`channel_id`) references `faq_channels` (`id`) on delete cascade)

你知道为什么吗?

// create_faq_channels_table
public function up()
{
    Schema::create('faq_channels', function (Blueprint $table) {
        $table->id();
        $table->string('channel');
        $table->text('description');
        $table->timestamps();
    });
}

// create_faq_questions_table
Schema::create('faq_questions', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('channel_id');
    $table->string('question');
    $table->text('response');

    $table->timestamps();

    $table->foreign('channel_id')->references('id')->on('faq_channels')->onDelete('cascade');

});

【问题讨论】:

    标签: laravel


    【解决方案1】:

    我有这 两个 迁移文件,一个用于创建 faq_channels 表 另一个用于创建 faq_questions 表。

    确保“faq_channels”表的迁移文件在“faq_questions”表的迁移文件之前运行。一般来说,在声明外键约束之前没有创建父表时会出现这种类型的错误。

    同时根据 Talha F. 的回答编辑您的迁移文件。

    【讨论】:

      猜你喜欢
      • 2021-07-06
      • 2018-10-04
      • 2011-01-29
      • 1970-01-01
      • 2017-05-06
      • 2020-03-23
      • 2021-08-31
      • 2021-04-24
      • 1970-01-01
      相关资源
      最近更新 更多