dev*_*evo 8 migration codeigniter codeigniter-2 dbforge
如何financial_year使用Codeigniter 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
}
Run Code Online (Sandbox Code Playgroud)
pge*_*e70 10
在数据库驱动程序中没有选项可以执行此操作.
或者编写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`)');
Run Code Online (Sandbox Code Playgroud)
小智 7
这个问题标记为CodeIgniter2 - @ pgee70答案是正确的
但
CodeIgniter3支持创建唯一字段.只需添加
'unique' => TRUE
Run Code Online (Sandbox Code Playgroud)
到了现场
在这种情况下:
'financial_year' => array(
'type' => 'VARCHAR',
'constraint' => 20,
'unique' => TRUE
),
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6292 次 |
| 最近记录: |