I have the following migration in my Laravel migrations folder that I have already run:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAdminTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create('Admin' , function($table){
$table->increments('id');
$table->mediumText('title');
$table->text('blog_content');
$table->char('tag' , 15);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::drop('Admin');
}
}
The above migration is for my admin
table, what I would really like to do is add a foreign key in my admin
table that is associated with my tags
table. Something like:
$table->foreign('tag')->references('tag')->on('tags');
How do I do this now that I have already run my migration?
Edit
I tried the following:
STEP 1: deleted the tag
column from the admin
table from phpMyAdmin.
STEP 2: Tried running the following migration:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddForeignKeyTagsColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('Admin', function (Blueprint $table) {
$table->char('tag' , 15)->after('slug');
$table->foreign('tag')->references('tag')->on('tags');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
// Schema::drop('Admin');
}
}
But I get the following error:
Why is the foreign key unable to be created?