CodeIgniter Forums
Why i cannot add foreign key in migration, it creates different one. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Why i cannot add foreign key in migration, it creates different one. (/showthread.php?tid=90359)



Why i cannot add foreign key in migration, it creates different one. - sarath_unrelax - 03-06-2024

Hello,
Hope you all doing well!,. I'm trying to modify an existing table to add a column which i need to be foreignkey  to other table but it not creating.

My code :

PHP Code:
<?php

namespace App\Database\Migrations;

use 
CodeIgniter\Database\Migration;

class 
AddLeaveApplicationIdToLeaveAdjustMentTable extends Migration
{
    public function up()
    {
        $fields = [

            'leave_application_id' => [
    
                
'type' => 'INT',
                'constraint' => 7,
                'unsigned' => true
            
],
        ];
    
        $this
->forge->addForeignKey('leave_application_id''leave_applications''id''''''leave_application_id_fk');
        $this->forge->addColumn('leave_adjustment'$fields);

    }

    public function down()
    {
        $this->forge->dropForeignKey('leave_application_id_fk''leave_adjustment');
        $this->forge->dropColumn('leave_adjustment''leave_application_id');

    }
    


This not creating foreignkey to the leave_applications table. instead it created "leave_adjustment_emp_id_foreign".

How i can solve this issue ?

This creates column called "leave_application_id" but not creating foreign key, what wrong im doing here ?


RE: Why i cannot add foreign key in migration, it creates different one. - kenjis - 03-06-2024

See https://codeigniter4.github.io/CodeIgniter4/dbmgmt/forge.html#adding-keys-to-a-table


RE: Why i cannot add foreign key in migration, it creates different one. - sarath_unrelax - 03-06-2024

(03-06-2024, 04:42 PM)kenjis Wrote: See https://codeigniter4.github.io/CodeIgniter4/dbmgmt/forge.html#adding-keys-to-a-table

So I need to add this also ?

PHP Code:
$this->forge->processIndexes('leave_adjustment');

I'm correct ?