Codeigniter: Migrations DBForge cannot set CURRENT_TIMESTAMP in default add field array

Created on 4 Jun 2017  路  2Comments  路  Source: bcit-ci/CodeIgniter

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.

A Database Error Occurred

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?

Duplicate

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', ), ) );

All 2 comments

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', ), ) );

Was this page helpful?
0 / 5 - 0 ratings

Related issues

it-can picture it-can  路  5Comments

Struki84 picture Struki84  路  7Comments

rmdhfz picture rmdhfz  路  3Comments

anharku picture anharku  路  3Comments

rjdjohnston picture rjdjohnston  路  4Comments