Adding TIMESTAMP_INIT as a Default Value in Declarative Schema


 Introduction:

When working with databases, it's often essential to track the timestamp of various operations such as record creation or modification. Magento 2, a popular e-commerce platform, introduced the Declarative Schema approach to simplify database setup and maintenance. In this article, we will explore how to add the TIMESTAMP_INIT value as a default value using the Declarative Schema in Magento 2.

Understanding Declarative Schema: Declarative Schema is a powerful feature in Magento 2 that allows developers to define and manage database structures using XML files. It provides a structured way of creating and updating database tables, columns, indexes, and other elements. The schema is defined in a declarative manner, which means developers specify what the database should look like, and Magento handles the necessary database operations automatically.

Adding TIMESTAMP_INIT as a Default Value: To add TIMESTAMP_INIT as a default value in the Declarative Schema, follow these steps:

Step 1: Create a new file or modify an existing one: In your Magento module, navigate to the etc/db_schema.xml file or create one if it doesn't exist.

Step 2: Define the table structure: Inside the <schema> tag, define your table structure using the <table> tag. For example, let's say we have a table called example_table with a column named created_at that we want to set with the TIMESTAMP_INIT value.

Step 3: Add the column declaration: Within the <table> tag, add a <column> tag for the created_at column. Specify the column name, type, and length as per your requirements.

xml
<column xsi:type="timestamp" name="created_at" on_update="false" nullable="false" comment="Created At"/>

Here, on_update="false" ensures that the created_at column won't update automatically when the record is modified.

Step 4: Set the default value: To set TIMESTAMP_INIT as the default value, include the default="TIMESTAMP_INIT" attribute in the <column> tag.

xml
<column xsi:type="timestamp" name="created_at" on_update="false" nullable="false" default="TIMESTAMP_INIT" comment="Created At"/>

Step 5: Complete the db_schema.xml file: Save the db_schema.xml file with the complete table and column definitions.

Step 6: Run the database upgrade: To apply the changes, run the following command from the Magento root directory:

arduino
bin/magento setup:upgrade

This command triggers the installation and upgrade process, which executes the Declarative Schema changes.

Conclusion: By following the steps outlined in this article, you can easily add the TIMESTAMP_INIT value as a default value in the Declarative Schema of your Magento 2 module. Leveraging Declarative Schema simplifies the management of database structures and ensures consistent database operations across different environments.

Magento 2 all types

 Magento 2 is available in different editions based on business needs. Here are all the types of Magento 2 : 1. Magento Open Source (former...

Popular Posts

Posts