일부 테이블에 외래 키를 추가했지만 다시 작동하지 않습니다.Laravel 외래 키를 추가하지 못했습니다.
Schema::table('users', function(Blueprint $table){
$table->integer('account_type')->unsigned()->change();
$table->foreign('account_type')
->references('id')
->on('account_types');
});
그것은 터미널에서이 3 오류가 발생합니다 :
[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `users` add constraint `users_account_type_foreign` foreign key (`account_type`) references `account_types` (`id`))
[Doctrine\DBAL\Driver\PDOException]SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
[PDOException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
나는이 문제를 해결하기 위해 필요한 어떤 생각? 내 사용자 테이블에서 account_type 필드를 클릭하고 account_types 테이블의 관련 계정 유형으로 가져올 수는 없지만 SequelPro를 사용하면됩니다.
'account_types '를 만든 후에'사용자'가 생성 되었습니까? 그렇다면'account_types' 이전 날짜를'users'보다 높게 변경하는 것이 좋습니다. – Classified
@Classified 모든 마이그레이션을 다시 실행하고 싶지 않기 때문에 라이브 서버에 데이터가 있습니다. 그 주위에 다른 방법이 없습니까? – Lovelock
'account_types' 테이블을'users' 테이블 전에 생성되도록 만들 수 있습니다. 그런 다음'users'를 수정하는 새로운 마이그레이션을 생성 한 다음 외부 관계를 추가 할 수 있습니다. – Classified