doudou2121 2019-03-04 11:56 采纳率: 50%
浏览 72
已采纳

Laravel外键不正确

I'm having some trouble with migrations in Laravel. I keep getting the error that the foreign key constraint is illformed. I can't seem to see what I do wrong either. Naming the migrations so they run in the right order is in place. I also tried running one and one migration, and it always stops on the second migration (municipalities).

What am I doing wrong here?

1st table: counties

    public function up()
    {
        Schema::create('counties', function (Blueprint $table) {
            $table->bigIncrements('id')->unsigned();
            $table->string('name');
            $table->timestamps();
        });
        DB::statement('ALTER TABLE counties CHANGE id id INT(2) UNSIGNED ZEROFILL NOT NULL');
    }

2nd table: municipalities

public function up()
{
    Schema::create('municipalities', function (Blueprint $table) {
        $table->bigIncrements('id')->unsigned();
        $table->bigInteger('county_id')->unsigned();
        $table->foreign('county_id')->references('id')->on('counties')->onDelete('cascade');
        $table->string('name');
        $table->timestamps();
    });
    DB::statement('ALTER TABLE municipalities CHANGE county_id county_id INT(2) UNSIGNED ZEROFILL NOT NULL');
    DB::statement('ALTER TABLE municipalities CHANGE id id INT(4) UNSIGNED ZEROFILL NOT NULL');
}

3rd table: postals

public function up()
{
    Schema::create('postals', function (Blueprint $table) {
        $table->bigIncrements('id')->unsigned();
        $table->bigInteger('municipality_id')->unsigned();
        $table->foreign('municipality_id')->references('id')->on('municipalities')->onDelete('cascade');
        $table->string('name');
        $table->timestamps();
    });
    DB::statement('ALTER TABLE postals CHANGE municipality_id municipality_id INT(4) UNSIGNED ZEROFILL NOT NULL');
    DB::statement('ALTER TABLE postals CHANGE id id INT(6) UNSIGNED ZEROFILL NOT NULL');
}

4th table: zips

public function up()
{
    Schema::create('zips', function (Blueprint $table) {
        $table->bigIncrements('id')->unsigned();
        $table->bigInteger('postal_id')->unsigned();
        $table->foreign('postal_id')->references('id')->on('postals')->onDelete('cascade');
        $table->decimal('lat', 12, 7);
        $table->decimal('lon', 12, 7);
        $table->timestamps();
    });
    DB::statement('ALTER TABLE zips CHANGE id id INT(4) UNSIGNED ZEROFILL NOT NULL');
    DB::statement('ALTER TABLE zips CHANGE postal_id postal_id INT(6) UNSIGNED ZEROFILL NOT NULL');
}

And the error message I'm getting: enter image description here

  • 写回答

1条回答 默认 最新

  • down100009 2019-03-04 15:18
    关注

    Apparantly I can't use bigInteger when creating the foreign keys. Switched to normal integer and it works

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作