Php Laravel 5.2删除外键
你好 我试图在以前实现的Laravel 5.2迁移中删除外键,如下所示:Php Laravel 5.2删除外键,php,mysql,laravel-5.2,Php,Mysql,Laravel 5.2,你好 我试图在以前实现的Laravel 5.2迁移中删除外键,如下所示: Schema::table('agents', function (Blueprint $table){ $table->dropForeign('agents_agent_rights_id_foreign'); $table->dropColumn('agent_rights_id'); }); $table->integer('agent_rights_i
Schema::table('agents', function (Blueprint $table){
$table->dropForeign('agents_agent_rights_id_foreign');
$table->dropColumn('agent_rights_id');
});
$table->integer('agent_rights_id')->unsigned(); // Always create column before creating Foreign key constraint otherwise this will also give error.
$table->foreign('agent_rights_id')->references('id')->on('agent_rights');
编辑:表中id的创建(在使用外键之前)是:
外键:
Schema::table('agents', function (Blueprint $table){
$table->foreign('agent_rights_id')->references('id')->on('agent_rights');
});
我的水滴看起来像这样:
Schema::table('agents', function (Blueprint $table){
$table->dropForeign('agents_agent_rights_id_foreign');
$table->dropColumn('agent_rights_id');
});
$table->integer('agent_rights_id')->unsigned(); // Always create column before creating Foreign key constraint otherwise this will also give error.
$table->foreign('agent_rights_id')->references('id')->on('agent_rights');
我发现,必须使用“真实”的索引名,而不是标签——我在前面的代码段中已经想到了这一点(作为参考)
但这给了我错误:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1025 Error on rename of './{name}/agents' to './{name}/#sql2-2a6-1d8' (errno: 152) (SQL: alter table `agents` drop foreign key `agents_agent_rights_id_foreign`)
[PDOException]
SQLSTATE[HY000]: General error: 1025 Error on rename of './{name}/agents' to './{name}/#sql2-2a6-1d8' (errno: 152)
研究这一点并没有带来真正的解决方案,只有来自MySQL的错误消息
问题:你们对此有何看法,或者我的代码段出了什么问题?@Mentenyia在创建外键约束之前,必须使用
unsigned()
。请阅读链接
注意:外键中引用的字段很可能是自动
递增,因此自动为无符号整数。请做
确保创建外键字段,并将unsigned()作为两个字段
必须是完全相同的类型,两个表上的引擎必须是
设置为InnoDB,则必须在
带有外键的表
因此,您必须这样创建:
Schema::table('agents', function (Blueprint $table){
$table->dropForeign('agents_agent_rights_id_foreign');
$table->dropColumn('agent_rights_id');
});
$table->integer('agent_rights_id')->unsigned(); // Always create column before creating Foreign key constraint otherwise this will also give error.
$table->foreign('agent_rights_id')->references('id')->on('agent_rights');
删除外键后,不会出现任何问题/错误
对于删除索引,请使用此方法表名\u列名\u索引类型
@Mentenyia在创建外键约束之前,必须使用
unsigned()
。请阅读链接
注意:外键中引用的字段很可能是自动
递增,因此自动为无符号整数。请做
确保创建外键字段,并将unsigned()作为两个字段
必须是完全相同的类型,两个表上的引擎必须是
设置为InnoDB,则必须在
带有外键的表
因此,您必须这样创建:
Schema::table('agents', function (Blueprint $table){
$table->dropForeign('agents_agent_rights_id_foreign');
$table->dropColumn('agent_rights_id');
});
$table->integer('agent_rights_id')->unsigned(); // Always create column before creating Foreign key constraint otherwise this will also give error.
$table->foreign('agent_rights_id')->references('id')->on('agent_rights');
删除外键后,不会出现任何问题/错误
对于删除索引,请使用此方法表名\u列名\u索引类型
在我写这篇文章之前,我做了这个问题中提到的事情。这不是同一个问题,因为我已经做了那里告诉我的事情。你遗漏了一些东西,因为我在我的项目中运行了相同的程序,并且运行良好。无论如何,我删除了副本并重新打开它。我还再次检查了你的脚本。请参阅Laravel文档。您还可以传递一个数组值,该数组值将在删除时自动使用常规约束名称,如
$table->dropForeign(['agent\u rights\u id')代码>@Manish我试过了,但遗憾的是同样的错误。。。在列成为外键(unsigned()除外)之前,我是否希望在列中添加一些特殊的内容?在创建本文之前,我按照问题中的说明做了。这不是同一个问题,因为我已经做了那里告诉我的事情。你遗漏了一些东西,因为我在我的项目中运行了相同的程序,并且运行良好。无论如何,我删除了副本并重新打开它。我还再次检查了你的脚本。请参阅Laravel文档。您还可以传递一个数组值,该数组值将在删除时自动使用常规约束名称,如$table->dropForeign(['agent\u rights\u id')代码>@Manish我试过了,但遗憾的是同样的错误。。。在列成为外键(unsigned()除外)之前,是否需要向列添加一些特殊的内容?执行此操作时,以及在unsigned()之后不使用nullable()时,仍然存在错误。。。但是我会从头开始重新设置整个数据库来测试它。然后我会回答:)是unsigned()之后的null()造成了问题。感谢您的帮助:)执行此操作时,以及在unsigned()之后不使用null()时,我仍然存在错误。。。但是我会从头开始重新设置整个数据库来测试它。然后我会回答:)是unsigned()之后的null()造成了问题。谢谢你的帮助:)