Home > Mobile >  How to change the foreign key reference table with Migration
How to change the foreign key reference table with Migration

Time:04-05

I had migrated a migration in Laravel which was like this:

$table->unsignedBigInteger('rel_seller_id')->nullable();
$table->foreign('rel_seller_id')->references('id')->on('seller');

Now I need to change the seller table to sellers table.

But don't know how to do this with Migration!

So if you know, let me know please...

CodePudding user response:

Just create a new migration.

...
public function up()
{
    Schema::table('table_name', function (Blueprint $table) {
        $table->dropForeign(['rel_seller_id']);

        $table->foreign('rel_seller_id')
            ->references('id')
            ->on('sellers');
    });
}
...

It will drop old foreign key and create a new one.

CodePudding user response:

You can always create a new migration file and after that drop the foreign

$table->dropForeign('your foreign')

Then create a new foreign right below it. Here you can see the reference https://laravel.com/docs/8.x/migrations#dropping-foreign-keys

  • Related