CodeIgniter Forums
Migration Foreign key problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Addins (https://forum.codeigniter.com/forumdisplay.php?fid=34)
+--- Thread: Migration Foreign key problem (/showthread.php?tid=89161)



Migration Foreign key problem - pippuccio76 - 01-17-2024

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 ?


RE: Migration Foreign key problem - davis.lasis - 01-17-2024

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