Codeigniter creating an ENUM field with dbforge

Joshua Hansen picture Joshua Hansen · Jan 5, 2014 · Viewed 10.3k times · Source

I create a ENUM field, here is my code:

$field['test'] = array(
  'type' => 'ENUM',
  'constraint' => array('a','b','c'),
  'default'=> "a"
);
$this->dbforge->add_field($field);
$this->dbforge->create_table('demo'); 

And I got a message:

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘Array) DEFAULT ‘a’ NOT NULL ) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci’ at line 2

CREATE TABLE ci_demo ( test ENUM(Array) DEFAULT ‘a’ NOT NULL ) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

Please help me, thank you very much.

Answer

M Khalid Junaid picture M Khalid Junaid · Jan 5, 2014

Try this one

$field['test'] = array(
'type' => 'ENUM("a","b","c")',
'default' => 'a',
'null' => FALSE,
);
$this->dbforge->add_field($field);
$this->dbforge->create_table('demo'); 

Reference