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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵