如何使用Codeigniter Dbforge迁移使financial_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
}发布于 2014-02-22 19:01:27
在数据库驱动程序中没有这样做的选项。
要么编写SQL来手动创建表,要么(这不是我推荐的)更改DB_forge.php和数据库驱动程序。(如mysql_forge.php)有唯一的键。看一下代码,我猜你只需要一个名为'unique_keys‘的类变量,然后按照代码中的’键‘和’主键‘来添加唯一键。
另一种选择是按原样使用dbforge,然后在创建后只修改表
$this->db->query('ALTER TABLE `financial_year` ADD UNIQUE INDEX (`financial_year`)');发布于 2016-09-25 00:19:38
此问题标记为CodeIgniter2 - @pgee70答案正确
但
CodeIgniter3支持创建唯一字段。只需添加
'unique' => TRUE到田里
在这种情况下:
'financial_year' => array(
'type' => 'VARCHAR',
'constraint' => 20,
'unique' => TRUE
),请参阅CodeIgniter user_guide
https://stackoverflow.com/questions/21074347
复制相似问题