외국 키를 떨어뜨릴 때의 문제
나의 외국 키는 자신의 테이블과 관련이 있습니다.이것은 위계가 있는 게시물을 만들기 위해서였습니다.
데이터베이스의 열을 삭제하려고 하면 다음과 같은 오류가 나타납니다.
1553 - Cannot drop index 'post_field_properties_parent_id_index': needed in a foreign key constraint
코드는 다음과 같습니다.
public function down()
{
Schema::table( "post_field_properties", function( $table )
{
$table->dropForeign('parent_id');
$table->dropColumn('parent_id');
} );
}
내가 할 수 있는 유일한 방법은 php my admin으로 가서 외국키 자체를 제거하는 것입니다.그 다음에 칼럼을 떨어트립니다.
그냥 내 프로젝트를 위해 이걸 알아냈어요.외부 키를 삭제할 때는 테이블 이름과 제약 조건의 열을 연결한 다음 이름을 "_foreign"으로 접미사를 붙여야 합니다.
http://laravel.com/docs/5.1/migrations#foreign-key-constraints
public function down()
{
Schema::table( "post_field_properties", function( $table )
{
$table->dropForeign('post_field_properties_parent_id_foreign');
$table->dropColumn('parent_id');
});
}
다음과 같은 방법이 있습니다.
- 데이터베이스에 로그인하고 외부 키 관계의 이름을 찾습니다.phphmyadmin을 사용하는 경우 테이블로 이동하여 "구조" 탭을 클릭하고 "관계 보기" 링크를 클릭합니다.
중요 참고: "관계 보기"를 보려면 표 "스토리지 엔진"이 다음과 같도록 하십시오.InnoDB본 질의응답을 검토하지 않을 경우
로딩될 때까지 몇 초 정도 기다립니다.Constraint name 필드를 검색합니다.예에서 다음과 같습니다. "contribution_copyright_id_foreign"
Laravel 마이그레이션 스크립트로 이동하거나 작성합니다.요령은 우선 외자 관계를 떨어뜨린 다음 열을 떨어뜨리는 것입니다.
공용 기능 저하
{
Schema::table('contribution', function(Blueprint $table){ $table->dropForeign('contribution_copyright_id_foreign'); $table->dropColumn('copyright_id'); });
외부 키가 있는 테이블을 제거하려면 먼저 외부 키 관계를 삭제해야 합니다.
여기서 복사한
누군가에게 도움이 되길 바랍니다.
라라벨 8을 사용하고 있는데, 알고 보니dropConstrainedForeignId우리가 이걸 위해 사용할 수 있는.따라서 이 답변 대신 다른 답변으로 제공되는 내용은 다음과 같습니다.
Schema::table( "post_field_properties", function( $table )
{
$table->dropForeign('post_field_properties_parent_id_foreign');
$table->dropColumn('parent_id');
});
다음과 같이 쓸 수 있습니다.
Schema::table( "post_field_properties", function( $table )
{
$table->dropConstrainedForeignId('parent_id');
});
삭제할 때 기존 제약 조건 이름을 자동으로 사용하는 배열 값을 전달할 수 있습니다. http://laravel.com/docs/5.1/migrations#foreign-key-constraints
Schema::table('contribution', function(Blueprint $table){
$table->dropForeign(['copyright_id']);
$table->dropColumn('copyright_id');
});
당신은 외부 키 배열을 내부에 전달할 수 있습니다.dropForeign()함수이므로, 라라벨은 키 이름의 시작 부분에 테이블 이름을 자동으로 연결하고 끝 부분에 'foreign'을 연결합니다.
그러니 다운() 기능에서 아래와 같은 코드를 사용해보세요.
Schema::table('contribution', function(Blueprint $table){
$table->dropForeign(['copyright_id']);
$table->dropColumn('copyright_id');
});
외부 키의 이름을 확인하려면 먼저 데이터베이스를 .sql로 백업합니다.
거기서 당신은 당신의 외국키의 이름을 다음과 같이 볼 것입니다.
...
KEY `employees_parent_id_foreign` (`parent_id`),
CONSTRAINT `employees_parent_id_foreign` FOREIGN KEY (`parent_id`) REFERENCES `laravel_article` (`id`) ON DELETE CASCADE
...
저의 경우 laravel 5.4, tablename_columnname_foreign 형식으로 시작합니다.
그래서 당신의 라라벨에서 (여기서 나는 직원 테이블에서 외국 키를 떨어뜨리려고 합니다)
Schema::table("employees", function( $table )
{
$table->dropForeign('employees_parent_id_foreign');
$table->dropColumn('parent_id');
});
열 이름 끝에 '_foreign'을 붙여 봅니다.예를 들어,
public function down()
{
Schema::table( "post_field_properties", function( $table )
{
$table->dropForeign('parent_id_foreign');
$table->dropColumn('parent_id');
});
}
언급URL : https://stackoverflow.com/questions/26628176/issue-with-dropping-foreign-key
'programing' 카테고리의 다른 글
| 배열에서 맵()을 자바스크립트로 역순으로 사용하는 방법이 있습니까? (0) | 2023.10.01 |
|---|---|
| 고정된 너비와 높이 내에 타원이 있는 크로스 브라우저 멀티 라인 텍스트 오버플로가 추가됩니다. (0) | 2023.10.01 |
| 자바스크립트 / jQuery에서 $.param( ) 역함수 (0) | 2023.10.01 |
| CSS 유닛 - vh/vw와 %의 차이점은 무엇입니까? (0) | 2023.10.01 |
| 전처리기를 이용한 문자열 연결 (0) | 2023.10.01 |