Created at and Updated at columns using db schema xml - Magento 2
-
27-03-2021 - |
Question
I have a custom table. Using the InstallSchema, I was able to add default values using:
)->addColumn(
'created_at',
\Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
null,
['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT],
'Created At'
)->addColumn(
'updated_at',
\Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
null,
['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT_UPDATE],
'Updated At'
How can i achieve the same thing using the db schema xml file? :
<table comment="my table comment" engine="innodb" name="my_table_name" resource="default">
......
<column name="created_at" nullable="false" xsi:type="datetime"/>
<column name="updated_at" nullable="true" xsi:type="datetime"/>
</table>
Solution 2
Eventually, I found out. Please see:
<column name="created_at" nullable="false" xsi:type="datetime" default="CURRENT_TIMESTAMP" on_update="false"/>
<column name="updated_at" nullable="true" xsi:type="datetime" default="CURRENT_TIMESTAMP" on_update="true"/>
I needed to add this part: default="CURRENT_TIMESTAMP"
OTHER TIPS
<table comment="my table comment" engine="innodb" name="my_table_name" resource="default">
<column xsi:type="timestamp" name="created_at" on_update="false" nullable="false" default="CURRENT_TIMESTAMP" comment="Created At"/>
<column xsi:type="timestamp" name="updated_at" on_update="true" nullable="false" default="CURRENT_TIMESTAMP" comment="Updated At"/>
</table>
use xsi:type="timestamp" . please implement and let me know if helpfull
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange