我有一個表已創建的外鍵約束:Laravel改變外鍵約束
$table->foreign('cms_id')->references('id')->on('inventories');
我需要,使其在inventories
表引用remote_id
而不是id
列改變這種外鍵。
我試圖通過這樣做:
public function up()
{
Schema::table('contents', function (Blueprint $table) {
$table->dropForeign('contents_cms_id_foreign');
$table->foreign('cms_id')->references('remote_id')->on('inventories');
});
}
但是,我得到:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL : alter tablecontents
add constraintcontents_cms_id_foreign
foreign k ey (cms_id
) referencesinventories
(remote_id
))[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
確保'cms_id'和'remote_id'具有相同的類型,大小和相同的屬性(如'UNSIGNED'等)。 – lesssugar
它們屬於同一類型,並且具有相同的屬性,但我仍然得到相同的錯誤 – Leff