CodeIgniter Forums
how to add index in addcolumn migration codeigniter 4 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: how to add index in addcolumn migration codeigniter 4 (/showthread.php?tid=81828)



how to add index in addcolumn migration codeigniter 4 - devo - 05-04-2022

PHP Code:
how can i add index on code_id and referral_code

<?php

namespace App\Database\Migrations;

use 
CodeIgniter\Database\Migration;

class 
AddColumUser extends Migration
{
    public function up()
    {
        $fields = [
            'fullname' => [
                'type' => 'VARCHAR',
                'constraint' => '100',
                'null' => false,
                'after' => 'balance',
            ],
            'phone' => [
                'type' => 'VARCHAR',
                'constraint' => '100',
                'null' => false,
                'after' => 'balance',
            ],
            'telegram' => [
                'type' => 'VARCHAR',
                'constraint' => '100',
                'null' => true,
                'after' => 'balance',
            ],
            'whatsapp' => [
                'type' => 'VARCHAR',
                'constraint' => '100',
                'null' => true,
                'after' => 'balance',
            ],
            'line' => [
                'type' => 'VARCHAR',
                'constraint' => '100',
                'null' => true,
                'after' => 'balance',
            ],
            'gender' => [
                'type' => 'ENUM',
                'constraint' => ['Male''Female'],
                'null' => true,
                'after' => 'balance',
            ],
            'address' => [
                'type' => 'VARCHAR',
                'constraint' => '255',
                'null' => true,
                'after' => 'balance',
            ],
            'postcode' => [
                'type' => 'VARCHAR',
                'constraint' => '50',
                'null' => true,
                'after' => 'balance',
            ],
            'state' => [
                'type' => 'VARCHAR',
                'constraint' => '150',
                'null' => true,
                'after' => 'balance',
            ],
            'referral_code' => [
                'type' => 'VARCHAR',
                'constraint' => '10',
                'null' => false,
                'after' => 'balance',
            ],
            'code_id' => [
                'type' => 'VARCHAR',
                'constraint' => '10',
                'null' => false,
                'after' => 'balance',
            ]
        ];
        $this->forge->addColumn('users'$fields);
        $this->forge->addKey(['referral_code''code_id']);
    }

    public function down()
    {
        $drop_colum = [
            'fullname',
            'phone'
            'telegram',
            'whatsapp',
            'line',
            'gender',
            'address',
            'postcode',
            'state',
            'referral_code',
            'code_id',
        ];
        $this->forge->dropColumn('users'$drop_colum);
    }




RE: how to add index in addcolumn migration codeigniter 4 - InsiteFX - 05-06-2022

Database Forge Class


RE: how to add index in addcolumn migration codeigniter 4 - kenjis - 05-06-2022

Missing: Means of adding indexes · Issue #4814 · codeigniter4/CodeIgniter4
https://github.com/codeigniter4/CodeIgniter4/issues/4814

Use query() method.
https://codeigniter.com/user_guide/database/queries.html#regular-queries


RE: how to add index in addcolumn migration codeigniter 4 - devo - 05-07-2022

(05-06-2022, 12:34 AM)InsiteFX Wrote: Database Forge Class

what did you try :
i try to add column and index to column code_id and referral_code
what did you get :
what i get is when i add column and index to my users table the column is added but index not added
what did you expect :
what i want is when i addcolum and index to table it must add the column and index the column

but i solve this problem alredy use query

(05-06-2022, 12:54 AM)kenjis Wrote: Missing: Means of adding indexes · Issue #4814 · codeigniter4/CodeIgniter4
https://github.com/codeigniter4/CodeIgniter4/issues/4814

Use query() method.
https://codeigniter.com/user_guide/database/queries.html#regular-queries

yes i solve this problem use query

PHP Code:
<?php

namespace App\Database\Migrations;

use 
CodeIgniter\Database\Migration;

class 
AddColumUser extends Migration
{
    public function up()
    {
        $fields = [
            'fullname' => [
                'type' => 'VARCHAR',
                'constraint' => '100',
                'null' => false,
                'after' => 'balance',
            ],
            'phone' => [
                'type' => 'VARCHAR',
                'constraint' => '100',
                'null' => false,
                'after' => 'balance',
            ],
            'telegram' => [
                'type' => 'VARCHAR',
                'constraint' => '100',
                'null' => true,
                'after' => 'balance',
            ],
            'whatsapp' => [
                'type' => 'VARCHAR',
                'constraint' => '100',
                'null' => true,
                'after' => 'balance',
            ],
            'line' => [
                'type' => 'VARCHAR',
                'constraint' => '100',
                'null' => true,
                'after' => 'balance',
            ],
            'gender' => [
                'type' => 'ENUM',
                'constraint' => ['Male''Female'],
                'null' => true,
                'after' => 'balance',
            ],
            'address' => [
                'type' => 'VARCHAR',
                'constraint' => '255',
                'null' => true,
                'after' => 'balance',
            ],
            'postcode' => [
                'type' => 'VARCHAR',
                'constraint' => '50',
                'null' => true,
                'after' => 'balance',
            ],
            'state' => [
                'type' => 'VARCHAR',
                'constraint' => '150',
                'null' => true,
                'after' => 'balance',
            ],
            'referral_code' => [
                'type' => 'VARCHAR',
                'constraint' => '10',
                'null' => false,
                'after' => 'balance',
            ],
            'code_id' => [
                'type' => 'VARCHAR',
                'constraint' => '10',
                'null' => false,
                'after' => 'balance',
            ]
        ];
        $this->forge->addColumn('users'$fields);
        $this->db->query("CREATE INDEX code_id ON users(code_id)");
$this->db->query("CREATE INDEX referral_code ON users(referral_code)");
    }

    public function down()
    {
        $drop_colum = [
            'fullname',
            'phone'
            'telegram',
            'whatsapp',
            'line',
            'gender',
            'address',
            'postcode',
            'state',
            'referral_code',
            'code_id',
        ];
        $this->forge->dropColumn('users'$drop_colum);
    }