Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.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
Mysql 无法将外键约束添加到透视表_Mysql_Database_Laravel_Laravel Migrations - Fatal编程技术网

Mysql 无法将外键约束添加到透视表

Mysql 无法将外键约束添加到透视表,mysql,database,laravel,laravel-migrations,Mysql,Database,Laravel,Laravel Migrations,我正在尝试使用外键创建透视表,这是我在Laravel中进行的迁移: public function up() { Schema::create('player_position', function (Blueprint $table) { $table->integer('player_id')->unsigned()->index(); $table->integer('position_id')->unsigned()-

我正在尝试使用外键创建透视表,这是我在Laravel中进行的迁移:

public function up()
{
    Schema::create('player_position', function (Blueprint $table) {
        $table->integer('player_id')->unsigned()->index();
        $table->integer('position_id')->unsigned()->index();

        $table->foreign('player_id')->references('id')->on('players')->onDelete('cascade');
        $table->foreign('position_id')->references('id')->on('positions')->onDelete('cascade');
    });
}
                                                                         [PDOException]                                                        
但是,我得到一个错误:

                                                                         [PDOException]                                                        
[照亮\数据库\查询异常]
SQLSTATE[HY000]:一般错误:1215无法添加外键约束 (SQL:alter table
player\u position
添加约束
player\u position\u position\u id\u foreign
外键(
position\u id
) 删除级联上的引用位置(
id

                                                                         [PDOException]                                                        
SQLSTATE[HY000]:一般错误:1215无法添加外键约束

                                                                         [PDOException]                                                        
我已经读到,通常外键约束错误是关于没有将未签名的字段分配给字段,或者数据库中已经有记录,但是我的数据库是空的,并且我的字段没有签名,所以不知道问题出在哪里

                                                                         [PDOException]                                                        
SQLSTATE[HY000]:一般错误:1215无法添加外键约束

                                                                         [PDOException]                                                        
要将字段定义为
外键
,引用的父字段上必须定义有
索引
。列的数据类型及其大小必须相同

                                                                         [PDOException]                                                        
我认为您违反了上面的一些规则。

删除->index()方法,因为它创建基本索引,而您希望添加引用另一个表上主键的外键约束

                                                                         [PDOException]                                                        
 $table->integer('player_id')->unsigned();
 $table->integer('position_id')->unsigned();

 $table->foreign('player_id')->references('id')->on('players')->onDelete('cascade');
 $table->foreign('position_id')->references('id')->on('positions')->onDelete('cascade');

我建议您不要对数据库使用约束。相反,试着在代码中管理它。@MortezaRajabi,为什么会这样?管理这样一个带有约束的表是很困难的,每次你想添加或删除某些内容时都会出现这些错误。这不是一个好的做法。但是,它们确实定义了索引,
数据类型如何,它们都是整数吗?它们都是相同类型和长度的整数吗?。例如,未签名和11个字符长。我有这个问题。将父id列从
$table->bigIncrements
更改为
$table->increments
(以匹配透视表上的
$table->integer
)对我来说很有效。