我们如何在 CodeIgniter 3 中通过迁移在现有 table 中添加列
How can we add column in existing table with migration in CodeIgniter 3
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Migration_Add_dummy_field_to_blog_table extends CI_Migration {
public function up()
{
$fields = array(
'dummy' => array('type' => 'TEXT')
);
$this->dbforge->add_column('blog', $fields);
}
public function down()
{
$this->dbforge->drop_column('blog', 'dummy');
}
}
首先您需要设置约束,然后指定您要添加列的位置。试试这个:
public function up()
{
$fields = array(
'dummy' => array(
'type' => 'varchar'
'constraint' => 100,
'after' => 'username'
)
);
$this->dbforge->add_column('blog', $fields);
}
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Migration_Add_dummy_field_to_blog_table extends CI_Migration {
public function up()
{
$fields = array(
'dummy' => array('type' => 'TEXT')
);
$this->dbforge->add_column('blog', $fields);
}
public function down()
{
$this->dbforge->drop_column('blog', 'dummy');
}
}
首先您需要设置约束,然后指定您要添加列的位置。试试这个:
public function up()
{
$fields = array(
'dummy' => array(
'type' => 'varchar'
'constraint' => 100,
'after' => 'username'
)
);
$this->dbforge->add_column('blog', $fields);
}