{"id":2732,"date":"2019-04-05T14:44:54","date_gmt":"2019-04-05T09:14:54","guid":{"rendered":"https:\/\/www.idslogic.com\/blog\/?p=2732"},"modified":"2020-12-09T09:36:06","modified_gmt":"2020-12-09T04:06:06","slug":"did-you-know-how-to-use-the-declarative-schema-in-magento-2-here-is-a-guide","status":"publish","type":"post","link":"https:\/\/www.idslogic.com\/blog\/did-you-know-how-to-use-the-declarative-schema-in-magento-2-here-is-a-guide","title":{"rendered":"Did You know How to Use the Declarative Schema in Magento 2? Here is a Guide"},"content":{"rendered":"<p><span style=\"font-size: 14pt;\">Magento includes this feature in its 2.3 version. It simplifies the Magento\u2019s installation and upgrade processes. Previously we had to write the database script for each version of the module. Various scripts were required for different operations on the database.<\/span><\/p>\n<blockquote><p><span style=\"color: #000080;\"><strong><span style=\"font-size: 14pt;\"><em>Declarative schema helps Magento to simplify module upgradation procedures. Natively each upgrade scripts iteratively adds changes to database blindly. Declarative Schema setup is based on database structure declarations.<\/em><\/span><\/strong><\/span><\/p><\/blockquote>\n<p><span style=\"font-size: 14pt;\">Declarative Schema files declare what the database structure should be, and Magento determines the differences between the current table structure and what it should be.<\/span><\/p>\n<blockquote><p><span style=\"font-size: 14pt; color: #000080;\"><em>Here, I will tell you how easily you can create a table using the declarative schema.<\/em><\/span><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<ol>\n<li><span style=\"font-size: 18pt; color: #008000;\"><strong> Create a db_schema.xml File inside etc Directory<\/strong><\/span><\/li>\n<\/ol>\n<pre><textarea class=\"xml\" name=\"code\">&lt;?xml version=\u201d1.0\u2033 encoding=\u201dUTF-8\u2033?&gt;\r\n\r\n&lt;schema xmlns:xsi=\u201dhttp:\/\/www.w3.org\/2001\/XMLSchema-instance\u201d xsi:noNamespaceSchemaLocation= \u201curn:magento:framework:Setup\/Declaration\/Schema\/etc\/schema.xsd\u201d&gt;\r\n\r\n&lt;\/p&gt;\r\n&lt;table name=\u201demp_data\u201d resource=\u201ddefault\u201d engine=\u201dinnodb\u201d comment=\u201dEmp table\u201d&gt;\r\n\r\n&lt;column xsi:type=\u201dsmallint\u201d name=\u201dentity_id\u201d padding=\u201d6\u2033 unsigned=\u201dfalse\u201d nullable=\u201dfalse\u201d identity=\u201dtrue\u201d comment=\u201dBOOK ID\u201d\/&gt;\r\n\r\n&lt;column xsi:type=\u201dsmallint\u201d name=\u201dentity_id\u201d padding=\u201d6\u2033 unsigned=\u201dfalse\u201d nullable=\u201dfalse\u201d identity=\u201dtrue\u201d comment=\u201dBOOK ID\u201d\/&gt;\r\n\r\n&lt;column xsi:type=\u201dvarchar\u201d name=\u201demp_name\u201d nullable=\u201dfalse\u201d length=\u201d255\u2033 comment=\u201dEmployee Name\u201d\/&gt;\r\n\r\n&lt;column xsi:type=\u201dint\u201d name=\u201dbook_id\u201d unsigned=\u201dtrue\u201d nullable=\u201dtrue\u201d identity=\u201dfalse\u201d comment=\u201dBook Id\u201d\/&gt;\r\n\r\n&lt;column xsi:type=\u201dtimestamp\u201d name=\u201dcreated_at\u201d on_update=\u201dfalse\u201d nullable=\u201dfalse\u201d default=\u201dCURRENT_TIMESTAMP\u201d comment=\u201dcreated at\u201d\/&gt;\r\n\r\n&lt;column xsi:type=\u201dvarchar\u201d name=\u201daddress\u201d nullable=\u201dtrue\u201d comment=\u201dAddress\u201d\/&gt;\r\n\r\n&lt;constraint xsi:type=\u201dprimary\u201d referenceId=\u201dPRIMARY\u201d&gt;\r\n\r\n&lt;column name=\u201dentity_id\u201d\/&gt;\r\n\r\n&lt;\/constraint&gt;\r\n\r\n&lt;\/table&gt;\r\n&lt;p&gt;\r\n\r\n&lt;\/schema&gt;&lt;span data-mce-type=\"bookmark\" style=\"display: inline-block; width: 0px; overflow: hidden; line-height: 0;\" class=\"mce_SELRES_start\"&gt;&lt;\/span&gt;\r\n\r\n\r\n\r\n\r\n<\/textarea><\/pre>\n<p><strong>A table node can contain three types of subnodes:<\/strong><\/p>\n<ol>\n<li>Column<\/li>\n<li>Constraint<\/li>\n<li>Index<\/li>\n<\/ol>\n<div style=\"margin: 40px auto; border: 2px solid #f46f25; padding: 20px 25px; border-radius: 6px; background: 0;\">\n<div style=\"display: inline-block; vertical-align: middle; width: 62%;\"><span style=\"font-size: 14pt;\"><strong>Are your ready to migrate your store to Magento 2?<\/strong><\/span><\/div>\n<div style=\"display: inline-block; vertical-align: middle; width: 36%;\"><span style=\"font-size: 14pt;\"><a style=\"padding: 10px 22px; margin-bottom: 0; line-height: 1.3em; text-align: center; text-decoration: none; vertical-align: middle; cursor: pointer; color: #ffffff; background-color: #f46f25; -moz-border-radius: 3px; -webkit-border-radius: 3px; border-radius: 3px; display: block;\" href=\"https:\/\/www.idslogic.com\/magento-1-to-magento-2-migration-service\/\" target=\"_blank\" rel=\"noopener noreferrer\">Call us today!<\/a><\/span><\/div>\n<\/div>\n<p><span style=\"font-size: 14pt; color: #008000;\"><strong>But before Running the Upgrade Command, You Need to Add Your Schema to db_schema_whitelist.json File by Running the Following Command-<\/strong><\/span><\/p>\n<p>php bin\/magento setup:db-declaration:generate-whitelist \u2013module-name=Ids_DeclarativeTest<\/p>\n<p>it will be look like this.<\/p>\n<pre><textarea class=\"xml\" name=\"code\">{\r\n\r\n\u201cemp_data\u201d: {\r\n\r\n\u201ccolumn\u201d: {\r\n\r\n\u201centity_id\u201d: true,\r\n\r\n\u201cemp_name\u201d: true,\r\n\r\n\u201cbook_id\u201d: true,\r\n\r\n\u201ccreated_at\u201d: true\r\n\r\n},\r\n\r\n\u201cconstraint\u201d: {\r\n\r\n\u201cPRIMARY\u201d: true\r\n\r\n}\r\n\r\n}\r\n\r\n}\r\n<\/textarea><\/pre>\n<p>When you will run s:up command table will be created.<\/p>\n<p>Hope this will help someone.<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 14pt; color: #800000;\"><strong>Contributed By<\/strong><\/span><\/p>\n<p><span style=\"color: #003366;\"><strong>Sharika Dubey<\/strong><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento includes this feature in its 2.3 version. It simplifies the Magento\u2019s installation and upgrade processes. Previously we had to write the database script for each version of the module. Various scripts were required for different operations on the database&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":2733,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[550],"tags":[795,442,265],"class_list":["post-2732","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-magento","tag-declarative-schema-in-magento-2","tag-magento-2-development","tag-magento-development"],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/posts\/2732","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/comments?post=2732"}],"version-history":[{"count":15,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/posts\/2732\/revisions"}],"predecessor-version":[{"id":2840,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/posts\/2732\/revisions\/2840"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/media\/2733"}],"wp:attachment":[{"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/media?parent=2732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/categories?post=2732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/tags?post=2732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}