我有两张桌子,一张优惠券和一张订单。凭证需要有16个字符的字符串作为ID,这是随机创建的。当ID作为整数递增时,一切都有效,但是当更改为字符串时,我无法让它添加外键,从而出现此错误:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL
: alter table `orders` add constraint `orders_voucher_id_foreign` foreign k
ey (`voucher_id`) references `vouchers` (`id`))
[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
以下是我的表格:
Schema::create('vouchers', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->string('id');
$table->integer('value');
$table->string('status');
$table->timestamps();
});
Schema::create('orders', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('event_id')->unsigned();
$table->string('voucher_id');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('event_id')->references('id')->on('events');
$table->foreign('voucher_id')->references('id')->on('vouchers');
});
任何反馈都会非常感激。提前谢谢。
答案 0 :(得分:3)
尝试:
$表 - >字符串(' ID') - >唯一的();