Laravel - Error al crear los Foreign key

 
Vista:
Imágen de perfil de Yoel
Val: 13
Ha disminuido su posición en 2 puestos en Laravel (en relación al último mes)
Gráfica de Laravel

Error al crear los Foreign key

Publicado por Yoel (8 intervenciones) el 19/09/2019 15:54:19
Tengo la siguiente situación estoy generado mis tablas desde laravel pero cuando corro el comando php artisan migrate me genera el siguiente error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Migrating: 2019_09_19_132437_create_tipogastos_table
 
   Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1005 Can't create table `laravel`.`tipogastos` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `tipogastos` add constraint `tipogastos_identidad_foreign` foreign key (`identidad`) references `entidades` (`id`))

  at /opt/lampp/htdocs/gastos/vendor/laravel/framework/src/Illuminate/Database/Connection.php:665
    661|         // If an exception occurs when attempting to run a query, we'll format the error
    662|         // message to include the bindings with SQL, which will make this exception a
    663|         // lot more helpful to the developer instead of just the database's errors.
    664|         catch (Exception $e) {
  > 665|             throw new QueryException(
    666|                 $query, $this->prepareBindings($bindings), $e
    667|             );
    668|         }
    669|
 
  Exception trace:
 
  1   PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `laravel`.`tipogastos` (errno: 150 "Foreign key constraint is incorrectly formed")")
      /opt/lampp/htdocs/gastos/vendor/laravel/framework/src/Illuminate/Database/Connection.php:459
 
  2   PDOStatement::execute()
      /opt/lampp/htdocs/gastos/vendor/laravel/framework/src/Illuminate/Database/Connection.php:459
 
  Please use the argument -v to see more details.

A continuación les dejo el código que estoy utilizando para ver si me pueden ayudar.
Gracias.

Tabla entidades

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public function up()
    {
        Schema::create('entidades', function (Blueprint $table) {
            $table->integer('id')->unsigned();
            $table->string('names',60);
            $table->string('address',255);
            $table->string('phone',15);
            $table->string('phone_two',15)->nullable();
            $table->string('email',100);
            $table->timestamps();
            $table->primary(array('id', 'email'));
        });
 
 
        DB::statement('ALTER TABLE entidades MODIFY id INTEGER NOT NULL AUTO_INCREMENT');
    }

Tabla tipo de gasto

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public function up()
    {
        Schema::create('tipogastos', function (Blueprint $table) {
            $table->integer('id')->unsigned();
            $table->integer('identidad')->unsigned();
            $table->string('names',60);
            $table->float('iva',4,2)->nullable();
            $table->char('tipo',1);
            $table->timestamps();
            $table->primary(array('id'));
        });
 
        DB::statement('ALTER TABLE tipogastos MODIFY id INTEGER NOT NULL AUTO_INCREMENT');
 
        Schema::table('tipogastos',function($table){
            $table->foreign('identidad')->references('id')->on('entidades');
        });
    }
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de kip
Val: 13
Ha aumentado su posición en 2 puestos en Laravel (en relación al último mes)
Gráfica de Laravel

Error al crear los Foreign key

Publicado por kip (3 intervenciones) el 19/09/2019 16:11:03
Hola, el problema es que el campo 'identidad' es unsigned y el campo 'id' de tipo gastos no lo es, puedes hacerlo asi y no tendras problemas:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public function up()
    {
        Schema::create('tipogastos', function (Blueprint $table) {
            $table->integer('id')->unsigned();
            $table->integer('identidad'); // Sin unsigned
            $table->string('names',60);
            $table->float('iva',4,2)->nullable();
            $table->char('tipo',1);
            $table->timestamps();
            $table->primary(array('id'));
        });
 
        DB::statement('ALTER TABLE tipogastos MODIFY id INTEGER NOT NULL AUTO_INCREMENT');
 
        Schema::table('tipogastos',function($table){
            $table->foreign('identidad')->references('id')->on('entidades');
        });
    }
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
Imágen de perfil de Yoel
Val: 13
Ha disminuido su posición en 2 puestos en Laravel (en relación al último mes)
Gráfica de Laravel

Error al crear los Foreign key

Publicado por Yoel (8 intervenciones) el 19/09/2019 21:58:58
Muchas gracias ya quedo.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar