40% de descuento en todos los planes

5d: 1h: 28m:53s'
Gabriel Villalon
Gabriel Villalon
Laravel
Comparte:

Foreign Error

Tengo un error al intentar agregar una foreign a una tabla para asociarla con otra, me podrian ayudar?
Esta es la tabla Empleados
public function up()
    {
        Schema::create('empleados', function (Blueprint $table) {
            $table->bigIncrements('id');

            $table->unsignedBigInteger('estudio_id');

            $table->integer('legajo');
            $table->binary('foto');
            $table->string('nombre');
            $table->string('apellido');
            $table->integer('dni');
            $table->string('cuil');
            $table->date('fecha_nacimiento');
            $table->integer('clase');
            $table->string('lugar_nacimiento');
            $table->integer('telefono');
            $table->string('estado_civil');
            $table->longText('observaciones');
            $table->boolean('estado');
            
            $table->timestamps();

            $table->foreign('estudio_id')->references('id')->on('estudios')
                        ->onDelete('cascade');
            
        });
    }

Esta es la Tabla Estudios
public function up()
    {
        Schema::create('estudios', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('nombre')->nullable();
            $table->timestamps();
        });
    }

y este es el error que tengo al hacer el migrate
   Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `empleados` add constraint `empleados_estudio_id_foreign` foreign key (`estudio_id`) references `estudios` (`id`) on delete cascade)

  at C:\laragon\www\Municipalidad\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   PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint")
      C:\laragon\www\Municipalidad\vendor\laravel\framework\src\Illuminate\Database\Connection.php:463

  2   PDOStatement::execute()
      C:\laragon\www\Municipalidad\vendor\laravel\framework\src\Illuminate\Database\Connection.php:463

  Please use the argument -v to see more details.
PS C:\laragon\www\Municipalidad>
Gracias ! 
Gabriel Villalon
Gabriel Villalon (0 xp)
Utilizo laravel 6.2 y PhpMyAdmin
Gabriel Villalon
Gabriel Villalon (0 xp)
Ya lo arregle, era el orden en que fueron creadas
1