【问题标题】:SQLSTATE[HY000]: General error: 1005 Can't create table `Data`.`company_eligibilities` (errno: 150 "Foreign key constraint is incorrectly formed")SQLSTATE [HY000]:一般错误:1005 无法创建表`Data`.`company eligibilities`(errno:150“外键约束形成错误”)
【发布时间】:2020-12-02 04:03:03
【问题描述】:
 public function up()
    {
        Schema::create('company_eligibilities', function (Blueprint $table) {
            $table->id();
            $table->string('marks');
            $table->integer('company_id')->unsigned();
            $table->timestamps();

            $table->foreign('company_id')
                ->references('id')->on('companies')
                ->onDelete('cascade')
                ->onUpdate('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('company_eligibilities');
    }
    
     

公司迁移在执行上述迁移之前已经完成

当我尝试与外键一起创建表时出现上述错误。我是不是做错了什么??

【问题讨论】:

    标签: laravel laravel-7 laravel-migrations


    【解决方案1】:

    确保在迁移company_eligibilities 之前迁移companies,将您的外键ID 更改为 无符号大整数,Laravel 5.8 将默认外键设为无符号大整数,6.x7.x 也一样:

    $table->bigInteger('company_id')->unsigned();
    

    或者,

    $table->unsignedBigInteger('company_id');
    

    【讨论】:

      猜你喜欢
      • 2020-12-19
      • 2021-09-23
      • 2021-08-06
      • 2020-11-27
      • 1970-01-01
      • 2020-08-07
      • 2017-08-20
      • 2022-01-25
      • 2018-02-04
      相关资源
      最近更新 更多