我正在尝试使用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_codes
(id
))
我正在使用MySQL作为数据库代码有什么问题?