Estou com erros ao tentar referenciar uma chave estrangeira. Tenho Autor e Livro, e não estou conseguindo colocar autor_id como chave estrangeira referenciando o id de Autor na migration do Livro
Meu código:
Migrations do Livro
public function up()
{
Schema::create('livros', function(Blueprint $table) {
$table->bigIncrements('id');
$table->string('title', 30);
$table->string('about', 30);
$table->string('company', 30);
$table->string('edition', 4);
#$table->bigInteger('autor_id');
$table->timestamps();
});
}
public function up()
{
Schema::table('livros', function(Blueprint $table) {
$table->bigInteger('autor_id');
$table->foreign('autor_id')->references('id')->on('autores');
});
}
Migration do Autor:
public function up()
{
Schema::create('autores', function(Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('surname');
$table->string('genre');
$table->integer('books_number');
$table->timestamps();
});
}
O erro:
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `livros` add constraint `livros_autor_id_foreign` foreign key (`autor_id`) references `autores` (`id`))
at /home/raiany/Documentos/Programming/Php/CRUD-books/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669
665| // If an exception occurs when attempting to run a query, we'll format the error
666| // message to include the bindings with SQL, which will make this exception a
667| // lot more helpful to the developer instead of just the database's errors.
668| catch (Exception $e) {
> 669| throw new QueryException(
670| $query, $this->prepareBindings($bindings), $e
671| );
672| }
673|
Exception trace:
1 Doctrine\DBAL\Driver\PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint")
/home/raiany/Documentos/Programming/Php/CRUD-books/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:123
2 PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint")
/home/raiany/Documentos/Programming/Php/CRUD-books/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:121