在Codeigniter DBForge迁移中创建唯一字段
如何使用在Codeigniter DBForge迁移中创建唯一字段,codeigniter,migration,codeigniter-2,dbforge,Codeigniter,Migration,Codeigniter 2,Dbforge,如何使用Codeigniter Dbforge迁移使financial\u year字段唯一 function up() { $this->dbforge->add_field(array( 'id' => array( 'type' => 'INT', 'constraint' => 11, 'unsigned'
Codeigniter Dbforge
迁移使financial\u year
字段唯一
function up() {
$this->dbforge->add_field(array(
'id' => array(
'type' => 'INT',
'constraint' => 11,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'financial_year' => array(
'type' => 'VARCHAR',
'constraint' => 20
),
'start_date' => array(
'type' => 'DATE'
),
'end_date' => array(
'type' => 'DATE'
),
'status' => array(
'type' => "ENUM",
'constraint' => "'Active','Inactive'",
'default' => "Active"
),
'created_on' => array(
'type' => 'TIMESTAMP'
)
));
$this->dbforge->add_key('id', TRUE); // add `id` as primary key
$this->dbforge->create_table('financial_year'); // create table schema
}
在数据库驱动程序中没有这样做的选项 要么编写SQL以手动创建表,要么(我不建议这样做)更改DB_forge.php和数据库驱动程序。(例如mysql_forge.php)具有唯一的密钥。看看代码,我想你只需要一个名为“unique_keys”的类变量,然后按照代码中的“keys”和“primary keys”添加唯一的键 另一种选择是按编写的方式使用dbforge,然后在创建表后修改表
$this->db->query('ALTER TABLE `financial_year` ADD UNIQUE INDEX (`financial_year`)');
此问题标记为CodeIgniter2-@pgee70答案正确 但是 CodeIgniter3支持创建唯一字段。 加上 到野外去 在这种情况下:
'financial_year' => array(
'type' => 'VARCHAR',
'constraint' => 20,
'unique' => TRUE
),
看
是否应该是
$this->db->query
?这破坏了我的迁移正确-应该是:$This->db->query('ALTER TABLE financial\u year ADD UNIQUE INDEX(financial\u year)')代码>这种情况下的向下迁移是:$this->db->query('ALTER TABLE financial\u year DROP INDEX(financial\u year)')代码>
'financial_year' => array(
'type' => 'VARCHAR',
'constraint' => 20,
'unique' => TRUE
),