Phinx: Default value now() for timestamp

Created on 16 Jun 2014  路  6Comments  路  Source: cakephp/phinx

Like to add a default value of now() to timestamp column. But could not do so.

Most helpful comment

I've just run into this issue and ended up here: http://stackoverflow.com/questions/168736/how-do-you-set-a-default-value-for-a-mysql-datetime-column

In my case the issue was due to being on MySQL < 5.6.5

To get around this I used a TIMESTAMP column instead:

public function change()
    {
        $table = $this->table('tablename');
        $table
            ->addColumn('columnName', 'timestamp', ['default' => 'CURRENT_TIMESTAMP'])
            ->save();
    }

Which worked as expected :)

All 6 comments

It appears from sources that you could specify: ['default' => 'CURRENT_TIMESTAMP']

This doesn't seem to be working. When trying to run a migration with ['default' => 'CURRENT_TIMESTAMP'], there is an error:

[PDOException]                                                                               
SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'created'

I'm using phinx 0.4.0 and mysql, in case this matters.

CURRENT_TIMESTAMP() maybe?

I've just used an sql query in the end :) but thanks.

I've just run into this issue and ended up here: http://stackoverflow.com/questions/168736/how-do-you-set-a-default-value-for-a-mysql-datetime-column

In my case the issue was due to being on MySQL < 5.6.5

To get around this I used a TIMESTAMP column instead:

public function change()
    {
        $table = $this->table('tablename');
        $table
            ->addColumn('columnName', 'timestamp', ['default' => 'CURRENT_TIMESTAMP'])
            ->save();
    }

Which worked as expected :)

In my case the issue was due to being on MySQL < 5.6.5

To get around this I used a TIMESTAMP column instead:
...
Which worked as expected :)

Yeah, the idea was to have a date or datetime column, not timestamp. But still - this is not a phinx issue, MySQL just can't do that.

Was this page helpful?
0 / 5 - 0 ratings