1

I want to add column that name 'accept_loc', type 'VARCHAR' to 3 database tables (use dbforge library from codeigniter) Assum they are named t1,t2,t3.

t1 structure:

  • id
  • bla_bla
  • pick_up_loc
  • ....

t2 structure:

  • id
  • pick_up_loc
  • ....

t3 structure:

  • id
  • bla_bla
  • pick_up_loc
  • ....

if I used this code below in my migration up function:

$fields = array(
        'accept_lat' => array(
            'accept_loc' => 'VARCHAR',
            'constraint' => '50',
            'after' => 'id',
        ),
    );
$this->dbforge->add_column('t1',$fields);
$this->dbforge->add_column('t2',$fields);
$this->dbforge->add_column('t3',$fields);

result:

t1 structure:

  • id
  • accept_loc
  • bla_bla
  • pick_up_loc
  • ....

t2 structure:

  • id
  • accept_loc
  • pick_up_loc
  • ....

t3 structure:

  • id
  • accept_loc
  • bla_bla
  • pick_up_loc
  • ....

it's not a good looking structure. How can I add column before 'pick_up_loc' to all of them (n database tables).

test test
  • 53
  • 1
  • 9

4 Answers4

1

Please try to put the after in uppercase, that is 'AFTER', there was a bug in that after key and mentioned got fixed here.

    $fields = array(
        'accept_lat' => array(
            'accept_loc' => 'VARCHAR',
            'constraint' => '50',
            'AFTER' => 'id'
        )
    );
Bira
  • 4,531
  • 2
  • 27
  • 42
1

The third argument in add_column() is $after_field. So you can't add the column before another column but you can do this:

$this->dbforge->add_column('t1', $fields, 'bla_bla');
$this->dbforge->add_column('t2', $fields, 'id');
$this->dbforge->add_column('t3', $fields, 'bla_bla');

Also, you seem to be naming the field 'accept_lat' and then using 'accept_loc' instead of the 'type' element which is confusing things.

richplane
  • 537
  • 4
  • 12
0

Late to the party but to clarify your code, on t1 for example, should look like this:

 $fields = array(
            'accept_lat' => array('type' => 'TEXT')
);
    $this->dbforge->add_column('t1', $fields, 'id');

So accept_lat will be added after the id column in table t1.

Bradley
  • 29
  • 6
0

From our assessment, it seems that the problem is unrelated to the functionality of our product.

Please refer to the documentation at https://codeigniter.com/userguide3/database/forge.html

Notice the following example provided in the documentation – it should help you resolve the issue.

$this->dbforge->add_field(""""label varchar(100) NOT NULL DEFAULT 'default label'"""");

If the issue still persists and is related to our software, please fill out the form below. Provide us with a detailed description of the problem and the actions you have taken. Also, attach relevant screenshots to aid us in better understanding the issue.

Note that you need to use the latest version of dbForge Studio.

Devart
  • 119,203
  • 23
  • 166
  • 186