如何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'); …Run Code Online (Sandbox Code Playgroud) 我创建了一个ENUM字段,这是我的代码:
$field['test'] = array(
'type' => 'ENUM',
'constraint' => array('a','b','c'),
'default'=> "a"
);
$this->dbforge->add_field($field);
$this->dbforge->create_table('demo');
Run Code Online (Sandbox Code Playgroud)
我收到一条消息:
错误号码:1064
您的SQL语法有错误; 检查与MySQL服务器版本对应的手册,以便在'Array'附近使用正确的语法DEFAULT'a'NOTE NULL)DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci'在第2行
CREATE TABLE
ci_demo(testENUM(Array)DEFAULT'a'NOT NULL)DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
请帮帮我,非常感谢你.
我正在使用Codeigniter的DBForge类在该数据库中创建数据库和表.
这是代码:
if ($this->dbforge->create_database('new_db')) {
$fields = array(
'blog_id' => array(
'type' => 'INT',
'constraint' => 5,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'blog_title' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
'blog_author' => array(
'type' => 'VARCHAR',
'constraint' => '100',
'default' => 'King of Town',
),
'blog_description' => array(
'type' => 'TEXT',
'null' => TRUE,
),
);
$this->dbforge->add_field($fields);
$this->dbforge->add_key('blog_id', TRUE);
$this->dbforge->create_table('blog', TRUE);
}
Run Code Online (Sandbox Code Playgroud)
上面提到的代码写在控制器的索引函数内.执行以下代码时,它显示错误,未选择数据库.有没有人知道为什么会这样.任何帮助表示赞赏.