douxia2053
douxia2053
2017-02-11 19:46

在Laravel中运行迁移后,将外键添加到表中

已采纳

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:

enter image description here

Why is the foreign key unable to be created?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongren5293 dongren5293 4年前

    Migrations are version control for your db. There is no point in writing migrations if you don't intend to use the version control functionality.

    So simply, you can alter the table by creating a new migration... from the command line:

    php artisan make:migration alter_table_admin_add_foreign_tag

    Then in your new migration, do your up and down methods.

    Once your migration is ready, php artisan migrate.

    REGARDING EDITED QUESTION:

    Both tables need to be innodb and not myisam. Further, both columns parent key and relation key need to be the same data type. As such, in your tags table is also configured with $table->char('tag' , 15);

    SIDE NOTE / suggestion:

    Why do you have some tables singular and some plural. and why do you have some tables starting with a capital letter, and other tables in lower case? Admin could be admins and tags could remain tags for consistency.

    点赞 评论 复制链接分享
  • douao3063 douao3063 4年前

    Create a new migration with the following code:

    public function up() {
        Schema::table('Admin' , function($table){
            $table->foreign('tag')->references('tag')->on('tags'); 
        });
    }
    
    public function down() {
        Schema::table('Admin' , function($table){
            $table->dropForeign('Admin_tag_foreign'); 
        });
    }
    

    Then run php artisan migrate

    点赞 评论 复制链接分享

相关推荐