Laravel迁移外键引用具有两列的主键?

时间:2016-09-28 09:43:00

标签: php mysql laravel-5 eloquent

我使用的是Laravel 5.3和MySQL。

如何将Laravel外键引用添加到具有两列的主键?

以下是我的迁移脚本(位于database/migrations/目录下)

有两列的主键

public function up()
{
    Schema::create('X', function (Blueprint $table) {
        $table->integer('a')->unsigned();
        $table->integer('b')->unsigned();
        $table->primary(['a', 'b']);
        $table->timestamps();
    });
}

在另一个,

public function up()
{
    Schema::create('Y', function (Blueprint $table) {
        $table->increments('k');
        $table->foreign('c')->references(['a', 'b'])->on('X')->onDelete('cascade');
        $table->timestamps();
    });
}

然而,它并没有这样做:我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:2)

在添加外国时,使用 Schema::table() 代替 Schema::create() 关键约束到您的数据库。

下面,说明修复的片段:

// File name: 2016_09_28_create_x_table.php
public function up()
{
    // Create table X
    Schema::create('X', function (Blueprint $table) {
        $table->integer('a')->unsigned();
        $table->integer('b')->unsigned();
        $table->primary(['a', 'b']);
        $table->timestamps();
    });
}


// File name: 2016_09_28_create_y_with_foreignkey_table.php
public function up()
{
    // Create table Y
    Schema::create('Y', function (Blueprint $table) {
        $table->increments('k');
        $table->integer('c')->unsigned();
        $table->timestamps();
    });

    // Add Foreign key
    Schema::table('Y', function (Blueprint $table) {
        $table->foreign('c')->references('a')->on('X')->onDelete('cascade');
    });
}

请记住 unsigned() 应该应用于 {{ 1}}

答案 1 :(得分:0)

这是我发现模拟复合键的唯一方法,而FK指向在Laravel 5.3中使用的复合键 - 我想念Laravel中更加紧凑的解决方案。

无论如何,这是我的代码

// File name: 2016_09_28_create_x_table.php
public function up()
{
    // Create table X
    Schema::create('X', function (Blueprint $table) {
        $table->increments('j');
        $table->integer('a')->unsigned();
        $table->integer('b')->unsigned();
        $table->unique(['a', 'b']);

        $table->timestamps();
    });
}


// File name: 2016_09_28_create_y_with_foreignkey_table.php
public function up()
{
    // Create table Y
    Schema::create('Y', function (Blueprint $table) {
        $table->increments('k');
        $table->integer('c')->unsigned();
        $table->timestamps();
    });

    // Add Foreign key
    Schema::table('Y', function (Blueprint $table) {
        $table->foreign('c')->references('j')->on('X')->onDelete('cascade');
    });
}

答案 2 :(得分:0)

Foreign key should be set for 2 columns

public function up()
{
    Schema::create('X', function (Blueprint $table) {
        $table->integer('a')->unsigned();
        $table->integer('b')->unsigned();
        $table->primary(['a', 'b']);
        $table->timestamps();
    });
}

public function up()
{
    Schema::create('Y', function (Blueprint $table) {
        $table->increments('k');
        $table->integer('a');
        $table->integer('b');
        $table->foreign(['a', 'b'])->references(['a', 'b'])->on('X')->onDelete('cascade');
        $table->timestamps();
    });
}`