Php SQLSTATE[HY000]:一般错误:1215无法添加外键约束Laravel 5.4
我有一个问题,我不能以任何方式解决拉威尔的迁移 这是我的用户迁移,数据名是:2014_12_10_000000_create_users_table.phpPhp SQLSTATE[HY000]:一般错误:1215无法添加外键约束Laravel 5.4,php,laravel,migration,laravel-5.4,Php,Laravel,Migration,Laravel 5.4,我有一个问题,我不能以任何方式解决拉威尔的迁移 这是我的用户迁移,数据名是:2014_12_10_000000_create_users_table.php Schema::create('users', function (Blueprint $table) { $table->engine = 'InnoDB'; $table->increments('id'); $table->unsignedIn
Schema::create('users', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->unsignedInteger('user_parent_id')->nullable();
$table->unsignedInteger('company_id')->nullable();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Schema::create('company', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
....
$table->timestamps();
});
这是公司表迁移,文件名为2018_06_01_080858_create_company_table.php
Schema::create('users', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->unsignedInteger('user_parent_id')->nullable();
$table->unsignedInteger('company_id')->nullable();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Schema::create('company', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
....
$table->timestamps();
});
这是一个设置外键的迁移,文件名为:2018_11_13_111338_alter_foreign_keys.php
Schema::table('users', function($table) {
$table->foreign('user_parent_id')->references('id')->on('users');
$table->foreign('company_id')->references('id')->on('company');
});
当我尝试运行php artisan:migrate时,总是出现以下错误:
In Connection.php line 647:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `users` add constraint `users_company_id_foreign` foreign key (`company_id`) references `company` (`id`))
In PDOStatement.php line 144:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
In PDOStatement.php line 142:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
最后,我为数据库中的所有其他表依次添加了名为2018_11_13_111338_alter_foreign_keys.php的迁移
欢迎提出任何建议,谢谢
这是AlterForeignable类的代码:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AlterForeignTable2 extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function($table) {
$table->foreign('user_parent_id')->references('id')->on('users');
$table->foreign('company_id')->references('id')->on('company');
});
Schema::table('otherstable', function($table) {
$table->foreign('user_id')->references('id')->on('users');
});
}
}
您有两个模型并添加了两个外来id。这是一个多对多的工作过程。在本文中,您只需要添加具有公司id和用户id的透视表
首先,您必须将用户id字段作为索引:
$table->index('user_id');
之后,您可以使用级联操作创建外键:
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
如果您想在新的迁移中做到这一点,您必须首先删除索引和外键,然后从头开始
在down()函数中,您必须执行此操作,然后在up()中执行我上面所写的操作:
$table->dropForeign('user_parent_id');
$table->dropIndex('lists_user_id_index');
$table->dropColumn('user_id');
试试这个,希望它能起作用
您是否试图将外键添加到已经有记录的表中?应该有公司@AmirhosseinDZ,而不是azienda
。@AmirhosseinDZ我已经纠正了这个问题,但这不是问题所在。@SasaBlagojevic我从空数据库开始,里面没有记录。@RobertoRemondini我已经创建了一个Laravel 5.4项目,您的迁移在我的本地环境中运行良好。一些容易被忽视的常见陷阱有1。迁移执行的顺序。2.主键列和外键列都必须是相同的类型,并且不带符号或带符号。因此,在运行迁移之前,请务必仔细检查此项,如果可能,请尝试删除整个数据库并创建一个新数据库。很抱歉,我不理解您的答复。为什么我不能有两个外键?你可以添加外键。你的关系,你说用逆运算,你需要用多对多的关系运算。这意味着很多公司都有很多用户,而且有很多回头客!!谢谢