Está acontecendo um erro quando vou fazer a migration um para muitos. O objetivo é fazer com que um cliente vai ter muitos telefones e um telefone vai pertencer apenas a um cliente. No código abaixo podem observar que o cliente_id referencia o campo id da tabela clientes.
Schema::create('telefones', function (Blueprint $table) {
$table->increments('id');
$table->integer('cliente_id')->unsigned();
$table->timestamps();
});
Schema::create('telefones', function (Blueprint $table) {
$table->foreign('cliente_id')->references('id')->on('clientes');
});
}
Mas quando vou executar o comando php artisan migrate
ele cria a tabela só que não tem o foreign key
e dá o seguinte erro:
PS C:\projetos\appteste> php artisan migrate
Migrating: 2019_07_28_221050_cria__tabela__telefones
Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL
syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') default character set utf8mb4 collate 'utf8mb4_unicode_ci'' at line 1 (SQL: create table `telefones` () default character set utf8mb4 collate 'utf8mb4_unicode_ci')
at C:\projetos\appteste\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the database's errors.
663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|
Exception trace:
1 Doctrine\DBAL\Driver\PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') default character
set utf8mb4 collate 'utf8mb4_unicode_ci'' at line 1")
C:\projetos\appteste\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php:63
2 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') default character set utf8mb4 collate 'utf8mb4_unicode_ci'' at line 1")
C:\projetos\appteste\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php:61