DBForge迁移中创建唯一字段

DBForge迁移中创建唯一字段

本文介绍了在Codeigniter DBForge迁移中创建唯一字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过Codeigniter Dbforge迁移使financial_year字段具有唯一性?

How can I make the financial_year field as unique using Codeigniter Dbforge migration?

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
}

推荐答案

数据库驱动程序中没有这样做的选项.

There is not an option to do that in the database driver.

要么编写SQL手动创建表,要么(或者我不建议这样做)更改DB_forge.php和您的数据库驱动程序. (例如mysql_forge.php)具有唯一键.查看代码,我想您将只有一个名为"unique_keys"的类变量,并按照代码在其中"keys"和"primary keys"的位置添加唯一键.

either write the SQL to create the table manually, or (and this isn't what i would recommend) change the DB_forge.php and your database driver. (eg mysql_forge.php) to have unique keys. looking at the code i guess you would just have a class variable called 'unique_keys' and follow the code where 'keys', and 'primary keys' are to add unique keys in.

另一种选择是使用dbforge,然后在创建后修改表

another option is to use the dbforge as written, then just modifiy the table after creation

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

这篇关于在Codeigniter DBForge迁移中创建唯一字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 15:34