douxia2053 2017-02-11 19:46
浏览 63
已采纳

在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 2017-02-11 19:52
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改