laravel migration best way to add foreign key

痴心易碎 提交于 2019-11-29 11:45:01

问题


Simple question: I'm new to Laravel. I have this migration file:

Schema::create('lists', function(Blueprint $table) {
    $table->increments('id'); 
    $table->string('title', 255);
    $table->integer('user_id')->unsigned(); 
    $table->foreign('user_id')->references('id')->on('users'); 
    $table->timestamps();
});

I want to update it to add onDelete('cascade').

What's the best way to do this?


回答1:


Firstly you have to make your user_id field an index:

$table->index('user_id');

After that you can create a foreign key with an action on cascade:

$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

If you want to do that with a new migration, you have to remove the index and foreign key firstly and do everything from scratch.

On down() function you have to do this and then on up() do what I've wrote above:

$table->dropForeign('lists_user_id_foreign');
$table->dropIndex('lists_user_id_index');
$table->dropColumn('user_id');



回答2:


$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');

In this example, we are stating that the user_id column references the id column on the users table. Make sure to create the foreign key column first! The user_id column is declared unsigned because it cannot have negative value.

You may also specify options for the "on delete" and "on update" actions of the constraint:

$table->foreign('user_id')
      ->references('id')->on('users')
      ->onDelete('cascade');

To drop a foreign key, you may use the dropForeign method. A similar naming convention is used for foreign keys as is used for other indexes:

$table->dropForeign('posts_user_id_foreign');

If you are fairly new to Laravel and Eloquent, try out the Laravel From Scratch series available on laracasts. It is a great guide for beginners.




回答3:


You should create a new migration file let's say 'add_user_foreign_key.php'

public function up()
{
    Schema::table('lists', function(Blueprint $table)
    {
         $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('lists', function(Blueprint $table)
    {
    $table->dropForeign('user_id'); //
    });
}  

The run

 php artisan migrate



回答4:


Schema::create('roles',function(Blueprint $table){

    $table->bigIncrements('id');
    $table->string('name');
    $table->timestamps();

});

Schema::create('permissions',function(Blueprint $table){

    $table->unsignedBigInteger('role_id');
    $table->foreign('role_id')->references('id')->on('roles');
    $table->string('permission');

});



回答5:


If you want to add onDelete('cascade'), just drop the indexes and create them again:

public function up()
{
    Schema::table('lists', function($table)
    {
        $table->dropForeign('lists_user_id_foreign');

        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    });
}

public function down()
{
    Schema::table('lists', function($table)
    {
        $table->dropForeign('lists_user_id_foreign');

        $table->foreign('user_id')->references('id')->on('users');
    });
}


来源:https://stackoverflow.com/questions/26437342/laravel-migration-best-way-to-add-foreign-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!