laravel迁移错误SQLSTATE [HY000]:常规错误:1215无法添加外键约束

问题描述 投票:0回答:1

我正在尝试使用laravel迁移进行外键约束

这是我的第一张桌子

public function up()
    {
        Schema::create('postal_codes', function (Blueprint $table) {
            $table->char('id', 5)->primary();
            $table->string('district', 150);
            $table->string('city', 150);
            $table->integer('province_id')->unsigned();
        });

        Schema::table('postal_codes', function ($table) {
            $table->foreign('province_id')->references('id')->on('provinces');
        });

    }

这是我的第二张桌子

public function up()
    {
        Schema::create('postal_code_details', function (Blueprint $table) {
            $table->id();
            $table->string('urban', 150);
            $table->char('postal_code', 5);
        });

        Schema::table('postal_code_details', function ($table) {
            $table->foreign('postal_code', 5)->references('id')->on('postal_codes');            
        });
    }

我跑步时

php artisan migrate:fresh

得到类似这样的错误:

SQLSTATE [HY000]:常规错误:1215无法添加外键约束(SQL:更改表postal_code_details添加约束5外键(postal_code)引用postal_codesid))

我正在使用MySQL作为数据库代码有什么问题?

php mysql laravel-5 artisan data-migration
1个回答
0
投票
尝试看看这个。我相信这是您正在寻找的答案
© www.soinside.com 2019 - 2024. All rights reserved.