Migration throws error in laravel 5.8.14
Closed this issue · 1 comments
saineshmamgain commented
Migration throws following error
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table user_voucher add constraint user_voucher_user_id_foreign foreign key (user_id) references users (id))
MySQL Version : mysql Ver 14.14 Distrib 5.7.25
saineshmamgain commented
I Solved the error by making some changes in the migration. Incase anybody needs help:
`Schema::create($voucherTable, function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('code', 32)->unique();
$table->morphs('model');
$table->text('data')->nullable();
$table->timestamp('expires_at')->nullable();
$table->timestamps();
});`
` Schema::create($pivotTable, function (Blueprint $table) use ($voucherTable) {
$table->bigIncrements('id');
$table->bigInteger('user_id', false, true);
$table->bigInteger('voucher_id', false, true);
$table->timestamp('redeemed_at');
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('voucher_id')->references('id')->on($voucherTable);
});
`