我有一个已经创建了外键约束的表:
$table->foreign('cms_id')->references('id')->on('inventories');
我需要更改此外键,以便它引用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');
});
}
但是,我明白了:
[照亮\数据库\ QueryException]
SQLSTATE [HY000]:常规错误:1215无法添加外键约束 (SQL:alter tablecontents
添加约束contents_cms_id_foreign
外国人(cms_id
)引用inventories
(remote_id
))[PDOException]
SQLSTATE [HY000]:常规错误:1215无法添加外键约束
答案 0 :(得分:2)
分两步添加新外键,除了分隔为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');
});
}