Lef*_*eff 3 php mysql foreign-keys laravel
我有一个已创建外键约束的表:
$table->foreign('cms_id')->references('id')->on('inventories');
Run Code Online (Sandbox Code Playgroud)
我需要更改此外键,以便它引用remote_id而不是表id中的inventories列。
我已经尝试过这样做:
public function up()
{
Schema::table('contents', function (Blueprint $table) {
$table->dropForeign('contents_cms_id_foreign');
$table->foreign('cms_id')->references('remote_id')->on('inventories');
});
}
Run Code Online (Sandbox Code Playgroud)
但是,我得到:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]:一般错误:1215 无法添加外键约束(SQL:alter tablecontentsadd constraintcontents_cms_id_foreign外键(cms_id)引用inventories(remote_id))[PDOException]
SQLSTATE[HY000]:一般错误:1215 无法添加外键约束
除了分离到之外,分两步添加新的外键Schema::table:
public function up()
{
Schema::table('contents', function (Blueprint $table) {
$table->dropForeign('contents_cms_id_foreign');
$table->integer('cmd_id')->unsigned();
});
Schema::table('contents', function (Blueprint $table) {
$table->foreign('cms_id')->references('remote_id')->on('inventories');
});
}
Run Code Online (Sandbox Code Playgroud)