2014-11-08 17 views

Odpowiedz

37

Najpierw powinniśmy upuścić klucz obcy. Dzięki Razor za tę wskazówkę

$table->dropForeign('answers_user_id_foreign'); 
$table->foreign('user_id') 
->references('id')->on('users') 
->onDelete('cascade'); 
+0

Czy odbywa się to w nowej migracji? –

+1

@BrentConnor Jeśli jest już migrowany i jest produkowany, tak. W przeciwnym razie możesz wycofać migrację, edytować oryginalny plik, a następnie przeprowadzić ponowną migrację. – Marcel

1
$table->foreign('user_id') 
     ->references('id')->on('users') 
     ->onDelete('cascade'); 
+0

[Illuminate \ Database \ QueryException] SQLSTATE [HY000]: Błąd ogólny: 1005 Nie można utworzyć tabeli "xxx. # Sql-5d7_226" (errno: 121) (SQL: alter table 'xxx' add constraint answer_user_id_foreign klucz obcy (' identyfikator_użytkownika ') odwołuje się' users'id ('id') przy kasowaniu kasowania) –

+1

To nie działa dla istniejącej kolumny –

+1

Może więc powinieneś zgłosić to jako błąd, ponieważ to jest oficjalna [udokumentowana metoda] (http://laravel.com/docs/4.2/schema#foreign-keys) –

3

laravel schematu budowniczy nie można zmodyfikować kolumny przy obecnym stanie, więc można używać surowych zapytań. Trzeba będzie usunąć i odtworzyć ograniczenie:

PostgreSQL

function up() 
{ 
    DB::statement('alter table answers drop constraint answers_user_id_foreign, 
        add constraint answers_user_id_foreign 
        foreign key (user_id) 
        references users(id) 
        on delete cascade;' 
    ); 
} 
function down() 
{ 
    DB::statement('alter table answers drop constraint answers_user_id_foreign, 
        add constraint answers_user_id_foreign 
        foreign key (user_id) 
        references users(id);' 
    ); 
} 

MySQL

function up() 
{ 
    DB::statement('alter table answers drop FOREIGN KEY answers_user_id_foreign;'); 
    DB::statement('alter table answers add constraint answers_user_id_foreign 
        foreign key (user_id) 
        references users(id) 
        on delete cascade;' 
    ); 
} 
function down() 
{ 
    DB::statement('alter table answers drop FOREIGN KEY answers_user_id_foreign;'); 
    DB::statement('alter table answers add constraint answers_user_id_foreign 
        foreign key (user_id) 
        references users(id);' 
    ); 
} 
0
$table->integer('user_id')->unsigned();  
$table->foreign('user_id') 
     ->references('id')->on('users') 
     ->onDelete('cascade'); 

jestem zakładając użyłeś Illuminate\Database\Schema\Blueprint::primary() stworzyć users.id. W takim przypadku numer users.id będzie niepodpisany. Dlatego kolumna klucza obcego user_id również musi być niepodpisana.

2

Dzięki za odpowiedź na pytanie. Pomóż mi dostać się do tego działającego kodu w L5.1:

public function up() 
{ 
    Schema::table('transactions', function (Blueprint $table) { 
     $table->dropForeign('transactions_order_id_foreign'); 
     $table->foreign('order_id') 
      ->references('id')->on('orders') 
      ->onDelete('cascade') 
      ->change(); 
    }); 

    Schema::table('orders', function (Blueprint $table) { 
     $table->dropForeign('orders_user_id_foreign'); 
     $table->foreign('user_id') 
      ->references('id')->on('users') 
      ->onDelete('cascade') 
      ->change(); 
    }); 
} 
Powiązane problemy