Migrations Laravel 5.8

Asked

Viewed 178 times

0

A mistake is happening when I’m going to make Migration one for many. The goal is to make a customer will have many phones and a phone will belong only to one customer. In the code below you can see that the cliente_id references the table id field.

    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');
    });
}

But when will I execute the command php artisan migrate it creates the table only it does not have the foreign key and gives the following error:

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

1 answer

0


You may not use the Schema::create twice for the same table. To add the foreign key you can use the same scope of the first create or use the Schema::table to make the changes.

Schema::create('telefones', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('cliente_id');
    $table->timestamps();
});

Schema::table('telefones', function (Blueprint $table) {
    $table->foreign('cliente_id')->references('id')->on('clientes');
});

You can change the two methods integer and unsigned - that work perfectly - to unsignedInteger which has the same result.

  • Make a mistake. Thank you very much!

  • No need to do 2 schema to relate could make the Foreign below unsigned

  • @Lucasantonio, that’s exactly what I described at the beginning of the answer. I prefer to suggest the creation of relationships in this way - separate - so it is easier to maintain.

  • I don’t think, I do together to expedite hit the eye already know where ta the relationship like this, so there I have to read one schema and then read another, processes and will be slower the Migration.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.