Solucionado (ver solução)
Solucionado
(ver solução)
4
respostas

Falha com MySQL

Arquivo episodios

 public function up()
    {
        Schema::create('episodios', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('numero');
            $table->unsignedBigInteger('temporada_id');

            $table->foreign('temporada_id')
                ->references('id')
                ->on('temporadas');

        });
    }

Arquivo temporadas

 public function up()
    {
        Schema::create('temporadas', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('numero');
            $table->unsignedBigInteger('serie_id');

            $table->foreign('serie_id')
                ->reference('id')
                ->on('series');
        });
    }

Resultado

jose@corei57600k:~/Desenvolvimento/Laravel/controle-series$ php artisan migrate
Migrating: 2020_05_29_021500_create_temporadas_table

   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 ')' at line 1 (SQL: alter table `temporadas` add constraint `temporadas_serie_id_foreign` foreign key (`serie_id`) references `series` ())

  at /home/jose/Desenvolvimento/Laravel/controle-series/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   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 ')' at line 1")
      /home/jose/Desenvolvimento/Laravel/controle-series/vendor/laravel/framework/src/Illuminate/Database/Connection.php:452

  2   PDO::prepare()
      /home/jose/Desenvolvimento/Laravel/controle-series/vendor/laravel/framework/src/Illuminate/Database/Connection.php:452

  Please use the argument -v to see more details.

O que pode estar ocorrendo?

4 respostas

Parece ser nesse bloco:

            $table->foreign('serie_id')
                ->reference('id')
                ->on('series');

Faltou o s de references.

Vê se é isso mesmo.

Corrigi, mas o erro continua...

Migrating: 2020_05_29_021500_create_temporadas_table

   Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 3780 Referencing column 'serie_id' and referenced column 'id' in foreign key constraint 'temporadas_serie_id_foreign' are incompatible. (SQL: alter table `temporadas` add constraint `temporadas_serie_id_foreign` foreign key (`serie_id`) references `series` (`id`))

  at /home/jose/Desenvolvimento/Laravel/controle-series/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   PDOException::("SQLSTATE[HY000]: General error: 3780 Referencing column 'serie_id' and referenced column 'id' in foreign key constraint 'temporadas_serie_id_foreign' are incompatible.")
      /home/jose/Desenvolvimento/Laravel/controle-series/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458

  2   PDOStatement::execute()
      /home/jose/Desenvolvimento/Laravel/controle-series/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458

  Please use the argument -v to see more details.

Provavelmente o código está com algum problema para mysql: 'serie_id' and referenced column 'id' in foreign key constraint 'temporadas_serie_id_foreign' are incompatible."), que não estou sabendo identificar.

solução!

O erro não continua. Agora o erro mudou.

Você está usando tipos diferentes na chave primária e estrangeira.

Chave primária provavelmente está como $table->bigIncrements('id');

Sua chaves estrangeira está como unsigned.

Bem, desisti de usar o mysql e migrei para o sqlite. Aí deu certo. Tem algum detalhe do Mysql ou uma chatice que passou desapercebida e não migra. A princípio, vou continuar o curso via sqlite e depois tento entender o que houve com o MySQL e o que deveria ter sido configurado para funcionar direito.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software