2017-06-21 34 views
1

を変更: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 table contents add constraint contents_cms_id_foreign foreign k ey (cms_id) references inventories (remote_id))

[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

+0

'cms_id'と' remote_id'の型とサイズが同じで、同じプロパティ(例えばUNSIGNEDなど)を持っていることを確認してください。 – lesssugar

+0

これらは同じ型で同じプロパティを持ちますが、同じエラーが発生します – Leff

答えて

0

はさておき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'); 
    }); 
} 
+0

db内にレコードがありません – Leff

+0

この場合、nullable()メソッドは不要です –

+0

しかし、 、私はまだ同じエラーが発生します – Leff