1

As I'm adding CURRENT_TIMESTAMP as a default value for created_datetime and updated_datetime columns in my database table named faqs, I'm getting both the columns updated while edit the FAQ in admin form.

I want to update timestamp only for updated_datetime column. So for that I'm trying to add TIMESTAMP_INIT as a default value for created_datetime column and TIMESTAMP_INIT_UPDATE for updated_datetime column as described follows:

app\code\Namespace\Module\etc\db_schema.xml

<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="faqs" resource="default" engine="innodb" comment="FAQ Table">
        ........
        <column xsi:type="timestamp" name="created_datetime" nullable="false" default="TIMESTAMP_INIT" comment="Created Datetime"/>
        <column xsi:type="timestamp" name="updated_datetime" nullable="false" default="TIMESTAMP_INIT_UPDATE" comment="Updated Datetime"/>
        ........
    </table>
</schema>

While running php bin/magento setup:upgrade command in cmd, I'm getting following error:

Element 'column', attribute 'default': [facet 'enumeration'] The value 'TIMESTAMP_INIT' is not an element of the set {'CURRENT_TIMESTAMP', '0', 'NULL'}.
Line: 12
Element 'column', attribute 'default': 'TIMESTAMP_INIT' is not a valid value of the atomic type 'defaults'.
Line: 12
Element 'column', attribute 'default': [facet 'enumeration'] The value 'TIMESTAMP_INIT_UPDATE' is not an element of the set {'CURRENT_TIMESTAMP', '0', 'NULL'}.
Line: 13
Element 'column', attribute 'default': 'TIMESTAMP_INIT_UPDATE' is not a valid value of the atomic type 'defaults'.
Line: 13

Can anyone please help me to correct the syntax or guide me how to achieve this in db_schema.xml? Thanks in anticipation.

1

You have to need update created_datetime and updated_datetime with on_update="false" and on_update="true" in db_schema.xml

app\code\Namespace\Module\etc\db_schema.xml

<column xsi:type="timestamp" name="created_datetime" nullable="false" default="CURRENT_TIMESTAMP" on_update="false" comment="Created Datetime"/>
<column xsi:type="timestamp" name="updated_datetime" nullable="false" default="CURRENT_TIMESTAMP" on_update="true" comment="Updated Datetime"/>

Hope it help!

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.