得到此错误1215。尽管类型和键名完全正确。我链接数据类型为varchar
的列,尽管很少使用varchar
列作为外键。但是我只需要添加它。下面给出了创建的两个表,并显示了它们的模式。
这是父表。
public function up()
{
Schema::create('resources', function (Blueprint $table) {
$table->increments('id');
$table->string('type');
$table->timestamps();
$table->softDeletes();
});
}
这是子表。
public function up()
{
Schema::create('active_subscriptions', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('resource_id');
$table->string('resource_type');
$table->dateTime('start_date');
$table->dateTime('end_date');
$table->decimal('net_price');
$table->foreign('resource_type')->references('type')->on('resources');
$table->timestamps();
$table->softDeletes();
});
}
这是我在这里遇到的错误。
1215 Cannot add foreign key constraint (SQL: alter table `active_subscriptions` add constraint `active_subscriptions_resource_type_foreign` foreign key (`resource_type`) references `resources` (`type`))
当外键的定义与参考键不同时,您可能会得到错误代码:1215无法添加外键约束。整数的大小和符号必须相同。字符串列,字符集和排序规则必须相同。