3

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>

2 Answers 2

8

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"

3
<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 it is helpful.

Your Answer

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

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