2014-01-12 47 views
6

我怎样才能让使用Codeigniter Dbforge迁移financial_year字段作为独特之处?在笨DBForge迁移创造出独特的领域

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 
} 

回答

8

在数据库驱动程序中没有这样的选项。

要么写SQL手动创建表,或(这是不是我会建议)改变DB_forge.php和你的数据库驱动程序。 (如mysql_forge.php)有独特的密钥。看看代码,我猜你只需要一个名为'unique_keys'的类变量,并按照代码'keys'和'primary keys'添加唯一键。

另一个选项是使用dbforge作为写的,然后就modifiy表创建

$this->db->query('ALTER TABLE `financial_year` ADD UNIQUE INDEX (`financial_year`)'); 
+2

它不应该是'''$这个 - > DB-> query' ''?这打破了我的移民 – sinhix

+0

正确的 - 它应该是:'$这个 - > DB->查询( 'ALTER TABLE financial_year ADD UNIQUE INDEX(financial_year)');' –

3

这个问题被标记为CodeIgniter2后 - @ pgee70答案是正确的

CodeIgniter3支持独特领域的创作。 只需添加

'unique'   => TRUE 

到外地

在这种情况下:

'financial_year' => array(
     'type'    => 'VARCHAR', 
     'constraint'  => 20, 
     'unique'   => TRUE 
    ), 

看到 CodeIgniter user_guide