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

报告相同问题?

悬赏问题

  • ¥250 雷电模拟器内存穿透、寻基址和特征码的教学
  • ¥200 比特币ord程序wallet_constructor.rs文件支持一次性铸造1000个代币,并将它们分配到40个UTXO上(每个UTXO上分配25个代币),并设置找零地址
  • ¥15 关于Java的学习问题
  • ¥15 如何使用chatgpt完成文本分类任务?
  • ¥15 已知速度v关于位置s的等式,怎么转化为已知位置求速度v的等式
  • ¥15 我有个餐饮系统,用wampserver把环境配置好了,但是后端的网页却进去,是为什么,能不能帮远程一下?
  • ¥15 R运行没有名称为"species"的插槽对于此对象类"SDMmodelCV"
  • ¥20 基于决策树的数字信号处理,2ask 2psk 2fsk的代码,检查下报错的原因
  • ¥15 wincc已组态的变量过多
  • ¥60 如图:直线与椭圆X轴平行,求直线与椭圆任意一点的相切坐标计算公式