php Laravel 迁移不会添加外键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22077573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:39:02  来源:igfitidea点击:

Laravel migration will not add foreign key

phplaravel

提问by arrowill12

I am new to migrations and attempting to create 2 tables with a foreign key in one referencing an id in the other but I am getting a general failure to add key error. is there something I am missing?

我是迁移的新手,并试图创建 2 个表,其中一个外键引用另一个中的 id,但我在添加键错误时遇到了一般性失败。有什么我想念的吗?

error:

错误:

[PDOException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

[PDOException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

code:

代码:

    Schema::create('app_groups', function($table) {
     $table->increments('id');
     $table->string('app_name');
     $table->unsignedInteger('app_group_id');
     $table->timestamps();
  });

  Schema::create('app_to_bucket', function($table) {
     $table->increments('id');
     $table->unsignedInteger('app_group_id');
     $table->unsignedInteger('bucket_id');
     $table->timestamps();
  });
  Schema::table('app_to_bucket', function($table) {
     $table->foreign('app_group_id')->references('app_group_id')->on('app_groups')->onDelete('cascade');
  });

回答by Todor Todorov

This will work for sure. Eloquent primary keys are integer with length 10 and unsigned. This is why the relation is not working.

这肯定会起作用。Eloquent 主键是长度为 10 且无符号的整数。这就是关系不起作用的原因。

Schema::create('app_groups', function($table) {
     $table->string('app_name');
     $table->integer('app_group_id')->length(10)->unsigned();
     $table->timestamps();
  });

  Schema::create('app_to_bucket', function($table) {
     $table->integer('app_group_id');
     $table->integer('bucket_id')->length(10)->unsigned();
     $table->timestamps();
  });
  Schema::table('app_to_bucket', function($table) {
     $table->foreign('app_group_id')->references('app_group_id')->on('app_groups')->onDelete('cascade');

回答by arrowill12

I have solved the problem.

我已经解决了这个问题。

The issue was that Laravel automatically assumes incrementing columns as the primary key. so I needed to specify that my app_group_idwas the primary key.

问题是 Laravel 自动假定递增列作为主键。所以我需要指定 myapp_group_id是主键。

 Schema::create('app_groups', function($table) {
     $table->string('app_name');
     $table->integer('app_group_id');
     $table->primary('app_group_id');
     $table->timestamps();
  });

  Schema::create('app_to_bucket', function($table) {
     $table->integer('app_group_id');
     $table->integer('bucket_id');
      $table->primary('bucket_id');
     $table->timestamps();
  });
  Schema::table('app_to_bucket', function($table) {
     $table->foreign('app_group_id')->references('app_group_id')->on('app_groups')->onDelete('cascade');
  });

回答by Janaka R Rajapaksha

foreign key and reference key should have same length and same type. if you set those keys satisfying that, the error will not popup :)

外键和引用键应该具有相同的长度和相同的类型。如果您设置满足这些要求的键,则不会弹出错误:)

回答by Dmitry

Ok, there are several problems you could meet while creating/adding foreign key constraints in MySQL-databases using Laravel.

好的,使用 Laravel 在 MySQL 数据库中创建/添加外键约束时可能会遇到几个问题。

First of all, you should check names of column's and table's you assign.

首先,您应该检查您分配的列和表的名称。

Secondly, check the database enginewhile creating the constraint. Refer to documentation https://dev.mysql.com/doc/refman/5.7/en/innodb-foreign-key-constraints.htmlit should be InnoDB.

其次,在创建约束时检查数据库引擎。请参阅文档https://dev.mysql.com/doc/refman/5.7/en/innodb-foreign-key-constraints.html它应该是InnoDB

Schema::create('app_groups', function($table) {
     // setting up the storage engine
     $table->engine='InnoDB';
     $table->increments('id');
     $table->integer('group_id')->unsigned();
     $table->string('app_name');
     $table->timestamps();
  });

Schema::create('app_to_bucket', function($table) {
     $table->engine='InnoDB';
     $table->increments('id');
     $table->integer('app_group_id')->unsigned();
     $table->integer('bucket_id')->unsigned();
     $table->timestamps();
     $table->foreign('app_group_id')
           ->references('group_id')
           ->on('app_groups')
           ->onDelete('cascade');
})
;}

Thirdly(optional), move your assigment of constraints(foreign keys, indexes and so on) to separate migration.

第三(可选),将约束(外键、索引等)的分配移动单独的迁移

回答by Antonio Carlos Ribeiro

You must first create the table, then create the foreign keys:

您必须首先创建表,然后创建外键:

Schema::create('app_to_bucket', function($table) {
    $table->increments('id');
    $table->integer('bucket_id')->unsigned();
    $table->integer('app_group_id')->unsigned();
    $table->timestamps();
});

Schema::table('app_to_bucket', function($table) {
    $table->foreign('app_group_id')->references('app_group_id')->on('app_groups')->onDelete('cascade');
});

回答by Morgan

Try this:

尝试这个:

Schema::create('app_groups', function($table) {
     $table->increments('id');
     $table->integer('group_id')->unsigned();
     $table->string('app_name');
     $table->timestamps();
  });
 Schema::create('app_to_bucket', function($table) {
     $table->increments('id');
     $table->integer('app_group_id')->unsigned();
     $table->integer('bucket_id')->unsigned();
     $table->timestamps();
     $table->foreign('app_group_id')->references('group_id')->on('app_groups')->onDelete('cascade');
  });