Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.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 拉维迁移。我无法添加引用到同一表的两个外键_Php_Mysql_Laravel_Migration - Fatal编程技术网

Php 拉维迁移。我无法添加引用到同一表的两个外键

Php 拉维迁移。我无法添加引用到同一表的两个外键,php,mysql,laravel,migration,Php,Mysql,Laravel,Migration,这是两个不同的文件。我在一个文件中创建表,在另一个文件中创建外键。问题是当我生成第二个外键时。当我只生成一个外键时,它工作正常,但当我生成第二个外键时,它会出错。知道我为什么不能使两个外键引用到同一个表吗?这是我收到的错误消息 Schema::table('student_groups', function (Blueprint $table) { $table->foreign('student_groups_year') ->references

这是两个不同的文件。我在一个文件中创建表,在另一个文件中创建外键。问题是当我生成第二个外键时。当我只生成一个外键时,它工作正常,但当我生成第二个外键时,它会出错。知道我为什么不能使两个外键引用到同一个表吗?这是我收到的错误消息

Schema::table('student_groups', function (Blueprint $table) {

        $table->foreign('student_groups_year')
        ->references('year')->on('groups');

        $table->foreign('student_groups_number')
        ->references('groups_number')->on('groups');
    });
SQLSTATE[HY000]:一般错误:1005无法创建表
bpo
\sql-26ec_995
(错误号:150“外键约束格式不正确”)(sql:alter table
学生组
添加约束
学生组
ps_学生组(编号)外键
外键(
学生组(编号))参考
组(
组(编号))

在Connection.php第458行:

SQLSTATE[HY000]:一般错误:1005无法创建表
bpo
\sql-26ec_995
(错误号:150“外键约束格式不正确”)


我找到了答案!!在任何地方都找不到这个,而且它很简单!您必须将其放入相同的外键代码中

In Connection.php line 664:
    $table->integer('student_groups_year')->unsigned();
    $table->integer('student_groups_number')->unsigned();

将外键更改为无符号整数您可以这样做

$table->foreign(['student_groups_number','student_groups_year'])
            ->references(['group_number','year'])->on('groups');
并确保不要将
varchar
类型用作外键

        $table->integer('student_groups_number')->unsigned();

希望这有帮助。

我找到了答案!!在任何地方都找不到这个,而且它很简单!您必须将其放入相同的外键代码中

In Connection.php line 664:
    $table->integer('student_groups_year')->unsigned();
    $table->integer('student_groups_number')->unsigned();

添加代码/文本而不是图像。问题出在“学生组”。我尝试将unsigned放在它上面,并将其更改为int,但仍然出现相同的错误:迁移表创建成功。在Connection.php第664行中:SQLSTATE[HY000]:一般错误:1005无法创建表
bpo
\sql-26ec\u 4c
(错误号:150“外键约束格式不正确”)(sql:alter table
学生组
添加约束
学生组学生组外键(
student\u groups\u ar
)在Connection.php第458行中引用了
groups
ar
):SQLSTATE[HY000]:一般错误:1005无法创建表
bpo
(错误号:150“外键约束格式不正确”)@StianEriksen我已经更新了答案,你必须将每个外键列都创建为无符号整数,否则它将不起作用嗨,我尝试将其更改为无符号,但问题是它将外键改为int 10,但主键是int 11。因此它不允许我连接它们。但在我找到修复后,它仍然可以正常工作,即使没有未签名:)