mam user_id kolumny fk w moim stoleDodaj "ON DELETE CASCADE" do istniejącej kolumny w laravel
$table->foreign('user_id')->references('id')->on('users');
Dodam na kaskadzie usuwać funkcję do tej istniejącej kolumny. Jak mogę to zrobić?
mam user_id kolumny fk w moim stoleDodaj "ON DELETE CASCADE" do istniejącej kolumny w laravel
$table->foreign('user_id')->references('id')->on('users');
Dodam na kaskadzie usuwać funkcję do tej istniejącej kolumny. Jak mogę to zrobić?
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');
Czy odbywa się to w nowej migracji? –
@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
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
[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) –
To nie działa dla istniejącej kolumny –
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) –
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);'
);
}
$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.
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();
});
}
użyć zapytania surowego 'DB :: statement (" .. ")'. Jak nazywa się twój stół? – Razor
nazwa tabeli to "odpowiedzi" –