6

I am updating my table structure and everything work good, but default value for column created_at is 0000-00-00 00:00:00 again and again.

I want it to be equal to current time (CURRENT_TIMESTAMP);

Here is my current code:

$table->addColumn($tableBanners, 'created_at', [
    'type'     => Varien_Db_Ddl_Table::TYPE_TIMESTAMP,
    'nullable' => false,
    'comment'  => 'created_at',
    'default'  => 'CURRENT_TIMESTAMP'
]);

2 Answers 2

8

On 'default' section, change to Varien_Db_Ddl_Table::TIMESTAMP_INIT. So that:

'default'  => Varien_Db_Ddl_Table::TIMESTAMP_INIT

Ref: https://code007.wordpress.com/2014/04/26/magento-how-to-create-a-timestamp-table-column-with-current_timestamp-as-default-value/

6
  • 1
    Yes, I've just found this solution :) Aug 17, 2017 at 10:09
  • Please accept my answer then :)
    – PY Yick
    Aug 17, 2017 at 10:10
  • 4
    And for updated_at columns use Varien_Db_Ddl_Table::TIMESTAMP_INIT_UPDATE as default. Mar 23, 2018 at 13:21
  • @clockworkgeek : Actually now it seems to be Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT_UPDATE, thanks for the hint !
    – rou6e
    Feb 27, 2019 at 16:11
  • @AdrienFERREIRAAlioze - Yes it's all different now with Magento 2. Mar 1, 2019 at 9:17
-2

You can use these for equal value for current timestamp

date('Y-m-d G:i:s')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.