Welcome Guest, Not a member yet? Register   Sign In
how to add index in addcolumn migration codeigniter 4
#1

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

Reply
#2

Database Forge Class
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

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

Use query() method.
https://codeigniter.com/user_guide/datab...ar-queries
Reply
#4

(This post was last modified: 05-07-2022, 02:48 AM by devo.)

(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/CodeIgni...ssues/4814

Use query() method.
https://codeigniter.com/user_guide/datab...ar-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);
    }

Reply




Theme © iAndrew 2016 - Forum software by © MyBB