Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel迁移外键引用具有两列的主键?_Php_Mysql_Laravel 5_Eloquent - Fatal编程技术网

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

Php Laravel迁移外键引用具有两列的主键?,php,mysql,laravel-5,eloquent,Php,Mysql,Laravel 5,Eloquent,我正在使用Laravel5.3和MySQL 如何将Laravel外键引用添加到具有两列的主键 下面是我的迁移脚本(在数据库/migrations/目录下): 具有两列的主键 public function up() { Schema::create('X', function (Blueprint $table) { $table->integer('a')->unsigned(); $table->integer('b')->un

我正在使用Laravel5.3和MySQL

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

下面是我的迁移脚本(在
数据库/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();
    });
}
但是,它不起作用:如何实现这一点?

在向数据库添加外键约束时,使用
而不是
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()
应该应用于
c

这是我发现的唯一一种模拟复合键和指向在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');
    });
}

我已经照你写的做了,但没有成功。你试过这种方法吗?有什么想法吗?非常感谢!更新了我的答案。[Illumb\Database\QueryException]SQLSTATE[HY000]:一般错误:1005无法创建表“MYDB.#sql-552_2c”(错误号:150)(SQL:alter table
Y
add constraint
Y\u id\u c\u foreign
外键(
c
)引用了
X
a
b
)的删除级联),我还收到了另一个错误[PDOException]SQLSTATE[HY000]:一般错误:1005无法创建表“MYDB.#sql-552_2c”(错误号:150)之后,我做了php artisan迁移:刷新我提交的上一次更新;
$table->foreign('c')->references('a')->on('X')->onDelete('cascade'));
;它工作正常,没有任何错误。您能为这段代码如何回答这个问题添加一些解释吗?它似乎与问题中提供的代码完全相同。这里有什么不同吗?如果有,您能解释一下您更改了什么,以及这是如何将外键引用添加到主键的吗?第二个表上的外键是重新设置的与2个字段相关感谢您在此处进行澄清,但您能否在此处提供您的答案并添加解释?高质量的答案可以提高所有寻找答案的程序员的投票率,此外,当您的答案明确时,您的未来可能会有更多的投票。
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();
    });
}`