3

I am using Magento 2.2.6 version. Am just trying to add an extra column updated_at in setup/upgradeschema.php. But on saving data, it is not updating the timestamp automatically. It is null always. Here is my code,

$installer->getConnection()->addColumn(
                'updated_at',
                \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                null,
                ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT_UPDATE],
                'Updated At'
            );

Adding the above code in

setup/InstallSchema.php

or

in upgradeschema[But only on creating the whole new table]

works fine. Can someone help?

0

I think because your default key using Uppercase, try change your code to be like this :

...
->addColumn(
        'updated_at',
        \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
        null,
        ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT_UPDATE],
        'Updated At'
...

if it does not work, you can see this link for more reference :

How do auto add datetime in InstallSchema magento2?

  • I tried giving 'DEFAULT' in lowercase too but doesn't work. I will edit my question to change 'DEFAULT' in lowercase. – Octopus Sep 21 '18 at 6:29
  • try to run bin/magento cache:flush in command line , if it still does not work I think you can try to follow this way magento.stackexchange.com/questions/84255/… – mrfizh Sep 21 '18 at 6:42
0

First of all the file name should be UpgradeSchema.php and in that file you need have a check like

if (version_compare($context->getVersion(), '<your_module_version>') < 0) { //your code }

Secondly you need to increment the module version in etc/module.xml and then run the bin/magento setup:upgrade
Now you can see your column is updated in the database.
If so, then you can see updated time in the column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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