当前位置: 首页 > 知识库问答 >
问题:

Laravel迁移错误号:150“外键约束格式不正确”

顾炎彬
2023-03-14

我有一个帖子表和一个到达表,它引用了flight no(文本字符串格式)作为外键。然而,当我运行Laravel迁移时,我得到了可怕的错误:

SQLSTATE[HY000]:常规错误: 1005不能创建表atc.#sql-2350_84(errno: 150"外键约束格式不正确")(SQL:改变表到达添加约束arrival_flightno_foreign外键(flight no)引用帖子flight no

[PDOExc0019]SQLSTATE[HY000]:常规错误: 1005不能创建表atc.#sql-2350_84(errno: 150"外键约束格式不正确")

职位

Schema::create('posts', function (Blueprint $table) {
    $table->increments('id');
    $table->string('flightno');
    $table->string('flighttype');
    $table->string('toa');
    $table->string('doa');
    $table->string('runway');
    $table->string('route');
    $table->string('parking');
    $table->timestamps();
}); 

到达

Schema::create('arrival', function (Blueprint $table) {
    $table->increments('id');
    $table->string('flightno');
    $table->string('cleaning');
    $table->string('rampservice');
    $table->string('waste');
    $table->string('deicing');
    $table->foreign('flightno')->references('flightno')->on('posts')->onDelete('cascade');
    $table->timestamps();
});

共有2个答案

薛经艺
2023-03-14

在外键列中使用unsignedBigintger以避免不匹配的外键数据类型问题。

例如,在arrival表中,使用flightno的类型作为unsignedBigInteger。

帖子:

Schema::create('posts', function (Blueprint $table) {
    $table->increments('id');
    $table->string('flightno');
    $table->string('flighttype');
    $table->string('toa');
    $table->string('doa');
    $table->string('runway');
    $table->string('route');
    $table->string('parking');
    $table->timestamps();
});

抵达:

Schema::create('arrival', function (Blueprint $table) {
    $table->increments('id');
    //---Change flightno type to unsignedBigInteger and if u faced any problem just use nullable at the end of flightno.
    $table->unsignedBigInteger('flightno');
    $table->string('cleaning');
    $table->string('rampservice');
    $table->string('waste');
    $table->string('deicing');
    $table->foreign('flightno')->references('flightno')->on('posts')->onDelete('cascade');
    $table->timestamps();
});

它将解决许多人面临的问题。

支嘉祥
2023-03-14

在我看来,您忘记了放置索引和设置flightno列的长度。这应该起作用:

职位

Schema::create('posts', function (Blueprint $table) {
    // ...
    $table->string('flightno', 30)->index();
    // ...
}); 

到达

Schema::create('arrival', function (Blueprint $table) {
    // ...
    $table->string('flightno', 30)->index();
    // ...
}); 
 类似资料:
  • 我有一个订单表和一个有一个引用作为一个外来的。然而,当我运行Laravel迁移我得到可怕的错误代码: SQLSTATE[HY000]:常规错误: 1005不能创建表.(errno: 150"外键约束格式不正确")(SQL:改变表add约束外键()引用() SQLSTATE[HY000]:常规错误: 1005不能创建表.(errno: 150"外键约束格式不正确") 这是我的表模式: 这是我的sch

  • 进行4次迁移,如下所示。这是用户表。 这就是艺术家的迁移。 这是歌曲迁移。 这就是专辑迁移。 这是一个连接众多艺术家和歌曲的特色。 当我尝试迁移这四个迁移时,会出现此错误。

  • 我试图执行此迁移: 用户: 文章: 但当我迁移时,会出现以下错误: SQLSTATE[HY000]:一般错误: 1005不能创建表.(Errcode: 150"外键约束是inc orrecly形成的")(SQL:更改表添加约束外键()引用()在更新级联上删除级联) 我已经尝试将大整数和大增量重命名为简单整数和增量,但没有成功。

  • 迁移我的数据库时,会出现此错误。下面是我的代码,后面是我在尝试运行迁移时遇到的错误。 密码 错误消息 [Illumb\Database\QueryException] SQLSTATE[HY000]:一般错误:1005无法创建表(errno:150“外键约束格式不正确”)(sql:alter 表添加约束MEINS\u category\u id\u外键()参考()关于删除级联)

  • 运行迁移命令时出现错误。 这是管理表模式 另一个是laravel安装附带的用户表。 我想为user_id创建一个外键,但它会出错

  • 我正在尝试迁移我的Rails MySQL数据库,但遇到以下错误: ActiveRecord::StatementInvalid:mysql2::错误:无法创建表。(错误号:150“外键约束格式不正确”):创建表(int AUTO_INCREMENT主键,varchar(255),int,int,datetime不为空, 以下是我的迁移: 如果有人能在这里给我一个主意,我将非常感谢。我已经搜索了一段