How to add addition column in sales_order table using db_schema or in Magento2.3.1 best way?
- use db_schema which is the easiest way to add columns or to delete column no need of script upgrade..aravind– aravind2019-07-09 09:19:19 +00:00Commented Jul 9, 2019 at 9:19
- but whole sales_order table need to define again in my script?Rutvee Sojitra– Rutvee Sojitra2019-07-09 09:20:54 +00:00Commented Jul 9, 2019 at 9:20
- Check Ref: codextblog.com/magento-2/…Rakesh Donga– Rakesh Donga2019-07-09 09:21:09 +00:00Commented Jul 9, 2019 at 9:21
- @RakeshDonga First try to understand my requirement. I am not asking for gridRutvee Sojitra– Rutvee Sojitra2019-07-09 09:22:33 +00:00Commented Jul 9, 2019 at 9:22
- @RutveeSojitra is it working for you?Rohan Hapani– Rohan Hapani2019-07-15 04:42:51 +00:00Commented Jul 15, 2019 at 4:42
2 Answers
Create a new module depending on Magento_Sales and create a db_schema.xml file to declare the to be created a column in it. Once it is declared, generate the db_schema_whitelist.json file and run setup:upgrade. (db_schema.xml file is introduced in magento2.3 to run DB command to create, update tables and columns and keys. There is no need not for setup install scripts for a table but yes to populate columns you still need to use the old methodology of setup upgradeSchema)
etc/module.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="FirstVendor_FirstModule" setup_version="1.1.12" /> <sequence> <module name="Magento_Sales"/> </sequence> registration.php
use \Magento\Framework\Component\ComponentRegistrar; ComponentRegistrar::register(ComponentRegistrar::MODULE, 'FirstVendor_FirstModule', __DIR__); 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="sales_order" resource="default" engine="innodb"> <column xsi:type="varchar" name="custom_column" length="100" unsigned="false" nullable="false" identity="false" comment="Custom Name"/> </table> </schema> Finally, run this below command to generate whitelist (Even though it's not mandatory. Without this, it will create column too but as a standard approach recommended.)
php bin/magento setup:db-declaration:generate-whitelist Once above command is executed, run the setup:upgrade to make it in effect.
php bin/magento setup:upgrade Let me know if this helps.
- Perform the same above steps for change quote column customer_note column type from varchar to text. But I got
Invalid Document Element 'column', attribute 'length': The attribute 'length' is not allowed.error while run setup:upgrade command. This error comes because of in magento quote module db_schema.xml file has this customer_note definition and it's type varchar and has a length. If remove length from that definition line then code working without any errorNimesh Patel– Nimesh Patel2020-02-21 19:40:14 +00:00Commented Feb 21, 2020 at 19:40
Try to use this below code :
app/code/RH/SalesCustom/etc/module.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="RH_SalesCustom" > <sequence> <module name="Magento_Sales"/> </sequence> </module> </config> app/code/RH/SalesCustom/registration.php
<?php use Magento\Framework\Component\ComponentRegistrar; ComponentRegistrar::register( ComponentRegistrar::MODULE, 'RH_SalesCustom', __DIR__ ); app/code/RH/SalesCustom/etc/db_schema.xml
<?xml version="1.0"?> <schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd"> <table name="sales_order"> <column xsi:type="int" name="custom_sales_id" padding="10" unsigned="true" nullable="false" comment="Custom Sales ID"/> <constraint xsi:type="foreign" referenceId="SALES_ORDER_REFERRED_BY_SALES_ORDER_ENTITY_ID" table="sales_order" column="custom_sales_id" referenceTable="sales_order" referenceColumn="entity_id" onDelete="CASCADE"/> </table> </schema> - custom_sales_id : your custom column name
Then run following command to generate db_schema_whitelist.json
php bin/magento setup:db-declaration:generate-whitelist
Then, upgrade command execute :
php bin/magento s:up
- What's the use of padding?Mohit Rane– Mohit Rane2022-04-22 12:12:06 +00:00Commented Apr 22, 2022 at 12:12
- 1It's size of int field.Rohan Hapani– Rohan Hapani2022-04-24 08:15:25 +00:00Commented Apr 24, 2022 at 8:15
- then what about length? Isn't it the same with length as well?Mohit Rane– Mohit Rane2022-04-25 05:54:19 +00:00Commented Apr 25, 2022 at 5:54
- Please read this : devdocs.magento.com/guides/v2.4/extension-dev-guide/…Rohan Hapani– Rohan Hapani2022-04-25 06:14:46 +00:00Commented Apr 25, 2022 at 6:14