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条)

报告相同问题?

悬赏问题

  • ¥15 python怎么在已有视频文件后添加新帧
  • ¥20 虚幻UE引擎如何让多个同一个蓝图的NPC执行一样的动画,
  • ¥15 fluent里模拟降膜反应的UDF编写
  • ¥15 MYSQL 多表拼接link
  • ¥15 关于某款2.13寸墨水屏的问题
  • ¥15 obsidian的中文层级自动编号
  • ¥15 同一个网口一个电脑连接有网,另一个电脑连接没网
  • ¥15 神经网络模型一直不能上GPU
  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵