Ad

What Is The Name Of Foreign Key Constraint Generated By Laravel Migration

- 1 answer

I have created news table with CreateNewsTable migratin. the CreateNewsTable migration codes is:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateNewsTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('news', function (Blueprint $table) {
        $table->string('author');
        $table->string('title');
        $table->string('title_image_path');
        $table->string('summary');
        $table->text('body');
        $table->bigInteger('timestamp');
        $table->boolean('published');

        $table->primary(['author', 'title', 'timestamp']);
        $table->foreign('author')->references('id')->on('users');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('news');
}
}

Now I want to remove foreign key constraint but I don not know the name of foreign key constraint. please help me.thanks for your answer in advance.

Ad

Answer

You can find foreign keys in the INFORMATION_SCHEMA in the table KEY_COLUMN_USAGE

For example, to find foreign keys for the table news:

SELECT * FROM information_schema.KEY_COLUMN_USAGE where TABLE_NAME = 'news';

The name of the foreign key is in a column named CONSTRAINT_NAME

Ad
source: stackoverflow.com
Ad