Welcome Guest, Not a member yet? Register   Sign In
Error creating table with DBForge using enum
#1

I'm trying to figure out the proper format to use when defining ENUM fields in CI 3.

Under CI 2, I have the following field definition:

PHP Code:
   private $permissions_new_fields = array(
 
       'permission_id' => array(
 
           'type' => 'INT',
 
           'constraint' => 11,
 
           'auto_increment' => true,
 
           'null' => false,
 
       ),
 
       'name' => array(
 
           'type' => 'VARCHAR',
 
           'constraint' => 30,
 
           'null' => false,
 
       ),
 
       'description' => array(
 
           'type' =>'VARCHAR',
 
           'constraint' => 100,
 
           'null' => false,
 
       ),
 
       'status' => array(
 
           'type' => 'ENUM',
 
           'constraint' => "'active','inactive','deleted'",
 
           'default' => 'active',
 
           'null' => false,
 
       ),
 
   ); 

Then the migration looks something like this:
PHP Code:
       $this->dbforge->add_field($this->permissions_new_fields);
 
       $this->dbforge->add_key('permission_id'true);
 
       $this->dbforge->create_table($this->permissions_table); 

All of which worked fine (and I just dumped my test database and re-ran the migration to make sure it still works) under CI 2.

Under CI 3, using the MySQLi driver, the same migration file gives me the following:

Quote:A Database Error Occurred

Error Number: 1067

Invalid default value for 'status'

CREATE TABLE `permissions` ( `permission_id` INT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(30) NOT NULL, `description` VARCHAR(100) NOT NULL, `status` ENUM('\'active\',\'inactive\',\'deleted\'') NOT NULL DEFAULT 'active', CONSTRAINT `pk_permissions` PRIMARY KEY(`permission_id`) ) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_general_ci

Filename: /bonfire/migrations/003_Permission_system_upgrade.php

Line Number: 186

Although the error says "Invalid default value for 'status'", the issue seems to be that the constraint on the 'status' field has been defined incorrectly:
Code:
`status` ENUM('\'active\',\'inactive\',\'deleted\'') NOT NULL DEFAULT 'active'

I've tried several variations, including an array and defining the constraint as 'active,inactive,deleted', but each variation I've tried so far has given me a slightly different incorrect result. The output I'm expecting would look something like this:

Code:
`status` ENUM('active','inactive','deleted') NOT NULL DEFAULT 'active'
Reply
#2

Have you tried using a string instead of an array for the field definition? I know it should work with array like it did in v2, but maybe as a temp workaround...
http://www.codeigniter.com/user_guide/da...-as-fields
Reply
#3

Generally, I avoid using the string format unless absolutely necessary, which, so far, has only been the case for setting things like 'CURRENT_TIMESTAMP'. In this case, my work-around is to move the 'constraint' into the 'type' field, like this:

PHP Code:
       'status' => array(
 
           'type' => "ENUM('active','inactive','deleted')",
 
           'default' => 'active',
 
           'null' => false,
 
       ), 


Since ENUM is a MySQL-specific type anyway, it's not the worst work-around, but it would be nice if I could use the constraint.
Reply
#4

(04-20-2015, 06:20 AM)mwhitney Wrote: Generally, I avoid using the string format unless absolutely necessary, which, so far, has only been the case for setting things like 'CURRENT_TIMESTAMP'. In this case, my work-around is to move the 'constraint' into the 'type' field, like this:


PHP Code:
       'status' => array(
 
           'type' => "ENUM('active','inactive','deleted')",
 
           'default' => 'active',
 
           'null' => false,
 
       ), 


Since ENUM is a MySQL-specific type anyway, it's not the worst work-around, but it would be nice if I could use the constraint.

Was looking for a work around for this as well, and came to the same conclusion you did.

PHP Code:
$db_columns = array(
 
   'status' => array(
 
       'type'          => "ENUM('unlocked','locked')",
 
       'default'       => 'unlocked',
 
       'comment'       => 'Status of asset (Locked or Unlocked)'
 
   )
); 

And its the same thing for SET
Reply




Theme © iAndrew 2016 - Forum software by © MyBB