【问题标题】:On Laravel I get the error errno: 150 "Foreign key constraint is incorrectly formed"在 Laravel 我得到错误 errno: 150 "Foreign key constraint is wrongly forms"
【发布时间】:2019-03-01 23:08:53
【问题描述】:

我是 Laravel 的新手,我遇到了迁移问题。

表名拼写正确,但我仍然收到错误。

错误状态

SQLSTATE[HY000]:一般错误:1005 无法创建表 first_db.#sql-41c_2f (errno: 150 "外键约束是 格式不正确”) (SQL: alter table fees add constraint fees_academic_id_foreign 外键 (academic_id) 引用 academics (academic_id))

错误指向下面这个文件:

Schema::create('fees', function (Blueprint $table) {
    $table->increments('fee_id');
    $table->integer('academic_id')->unsigned;
    $table->integer('level_id')->unsigned;
    $table->integer('fee_type_id')->unsigned;
    $table->string('fee_heading',200)->nullable;
    $table->float('amount', 8, 2);
    $table->foreign('academic_id')->references('academic_id')->on('academics');
    $table->foreign('level_id')->references('level_id')->on('levels');
    $table->foreign('fee_type_id')->references('fee_type_id')->on('feestype');
});

是不是我做错了什么?

【问题讨论】:

  • 看起来academic_id 的约束抛出了这个。可能是academics 表不存在、没有该字段、具有该字段但具有不同的定义(char、signed int 等)或类似的东西?
  • 外键列和引用列必须是相同的数据类型,并且在你的迁移$table->integer('academic_id')->unsigned()这样使用无符号

标签: php laravel


【解决方案1】:

无符号应该是一个函数:

$table->integer('academic_id')->unsigned();

【讨论】:

    【解决方案2】:

    这个外部列应该是无符号的。

    Schema::create('fees', function (Blueprint $table) {
        $table->increments('fee_id');
        $table->unsignedInteger('academic_id');
        $table->unsignedInteger('level_id');
        $table->unsignedInteger('fee_type_id');
        $table->string('fee_heading',200)->nullable();
        $table->float('amount', 8, 2);
    
        $table->foreign('academic_id')->references('academic_id')->on('academics');
        $table->foreign('level_id')->references('level_id')->on('levels');
        $table->foreign('fee_type_id')->references('fee_type_id')->on('feestype');
    });
    

    来源:Laravel Database: Migrations Columns

    【讨论】:

      猜你喜欢
      • 2020-08-11
      • 2021-08-31
      • 2021-04-24
      • 2022-08-22
      • 1970-01-01
      • 2020-08-07
      • 2011-05-03
      • 2016-12-27
      • 2018-10-11
      相关资源
      最近更新 更多