When I try to add current timestamp as datetime default
$this->dbforge->add_field(array(
'created_at' => array(
'type' => 'DATETIME',
'default' => 'CURRENT_TIMESTAMP',
),
));
I get this error message.
Error Number: 1067
Invalid default value for 'created_at'
CREATE TABLE `crud` ( `id` INT(9) NOT NULL AUTO_INCREMENT, `some_date` DATETIME NOT NULL DEFAULT 'CURRENT_TIMESTAMP', CONSTRAINT `pk_crud` PRIMARY KEY(`id`) ) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_general_ci
It looks like the CURRENT_TIMESTAMP gets wrapped with single quotes. I did this workaround instead
$this->dbforge->add_field("`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP");
Is there any way to do this in the array style?
See #4852.
this worked for me, and is valid in the documentation
$this->dbforge->add_field(
array(
'name' =>
array(
'type' => 'VARCHAR',
'constraint' => '150',
),
'created_at datetime default current_timestamp',
'updated_at datetime default current_timestamp on update current_timestamp',
'status' =>
array(
'type' => 'tinyint',
'constraint' => '1',
),
)
);
Most helpful comment
this worked for me, and is valid in the documentation
$this->dbforge->add_field( array( 'name' => array( 'type' => 'VARCHAR', 'constraint' => '150', ), 'created_at datetime default current_timestamp', 'updated_at datetime default current_timestamp on update current_timestamp', 'status' => array( 'type' => 'tinyint', 'constraint' => '1', ), ) );