Welcome Guest, Not a member yet? Register   Sign In
Migration Foreign key problem
#1

hi i try to create a migration with two foreign key .this is the code :
Code:
<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class DpiTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'          => 'INT',
                'unsigned'      => true,
                'auto_increment' => true,
            ],
            'nome' => [
                'type'      => 'VARCHAR',
                'constraint' => '100',
            ],
            'scadenza_giorni' => [
                'type' => 'INT',
                'null' => true,
            ],

            'tempo_minimo_riconsegna_giorni' => [
                'type' => 'INT',
                'null' => true,
            ],

            'created_at datetime default current_timestamp',
            'updated_at datetime default NULL',
            'deleted_at datetime default NULL',
        ]);
        $this->forge->addKey('id', true);
        $this->forge->createTable('dpi');
       
    }

    public function down()
    {
        $this->forge->dropTable('dpi');
    }
}

Code:
<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class GruppoTable extends Migration
{
    public function up()
    {
       

        $this->forge->addField([
            'id' => [
                'type'          => 'INT',
                'unsigned'      => true,
                'auto_increment' => true,
            ],
            'nome' => [
                'type'      => 'VARCHAR',
                'constraint' => '100',
            ],


            'created_at datetime default current_timestamp',
            'updated_at datetime default NULL',
            'deleted_at datetime default NULL',
        ]);
        $this->forge->addKey('id', true);
        $this->forge->createTable('gruppo');
       
    }

    public function down()
    {
        $this->forge->dropTable('gruppo');
    }
}

Code:
<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class GruppoDpiTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'          => 'INT',
                'unsigned'      => true,
                'auto_increment' => true,
            ],

            'id_gruppo' => [
                'type' => 'INT',
                'null' => false,
            ],

            'id_dpi' => [
                'type' => 'INT',
                'null' => false,
            ],



            'created_at datetime default current_timestamp',
            'updated_at datetime default NULL',
            'deleted_at datetime default NULL',
        ]);

        $this->forge->addForeignKey('id_gruppo','gruppo','id','NO ACTION','NO ACTION');
        $this->forge->addForeignKey('id_dpi','dpi','id','NO ACTION','NO ACTION');

        $this->forge->addKey('id', true);
        $this->forge->createTable('gruppo_dpi');
       
    }

    public function down()
    {
        $this->forge->dropTable('gruppo_dpi');
    }
}
When i run last migration :
Can't create table `vestiario_es_locale`.`gruppo_dpi` (errno: 150 "Foreign key constraint is incorrectly formed")
.How can i solve ?
Reply
#2

GruppoDpiTable
id_gruppo and id_dpi must have 'unsigned' => true,


PHP Code:
class GruppoDpiTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'          => 'INT',
                'unsigned'      => true,
                'auto_increment' => true,
            ],

            'id_gruppo' => [
                'type' => 'INT',
                'null' => false,
                'unsigned' => true,
            ],

            'id_dpi' => [
                'type' => 'INT',
                'null' => false,
                'unsigned' => true,
            ],
            'created_at datetime default current_timestamp',
            'updated_at datetime default NULL',
            'deleted_at datetime default NULL',
        ]);

        $this->forge->addForeignKey('id_gruppo','gruppo','id','NO ACTION','NO ACTION');
        $this->forge->addForeignKey('id_dpi','dpi','id','NO ACTION','NO ACTION');

        $this->forge->addKey('id'true);
        $this->forge->createTable('gruppo_dpi');
      
    
}

    public function down()
    {
        $this->forge->dropTable('gruppo_dpi');
    }

Reply




Theme © iAndrew 2016 - Forum software by © MyBB