I'm creating a Referral Program module, and I want to create a custom column inside the customer_entity table. The column is referred_by and it's a foreign key that as a relation with the customer_entity table itself. This column contains the id of the customer that sent the referral_code. How can I implement this? How can I add this field inside the customer_entity table? I try to add an attribute as static type, but the new column doesn't appear inside that table. I need to declere the db_schema.xml?

3 Answers 3


You can try using following code.



use Magento\Framework\Component\ComponentRegistrar;



<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Anshu_CustomerEdit" >
            <module name="Magento_Customer"/>


<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <table name="customer_entity">
        <column xsi:type="int" name="referred_by" padding="10" unsigned="true" nullable="false"
                comment="Referred By"/>
        <constraint xsi:type="foreign" referenceId="CUSTOMER_ENTITY_REFERRED_BY_CUSTOMER_ENTITY_ENTITY_ID" table="customer_entity"
                    column="referred_by" referenceTable="customer_entity" referenceColumn="entity_id" onDelete="CASCADE"/>

Then run following command to generate db_schema_whitelist.json

bin/magento setup:db-declaration:generate-whitelist --module-name Anshu_CustomerEdit

Then run bin/magento setup:upgrade command.

Here Anshu is the module namespace and CustomerEdit is the module name.

You can modify code according to your requirement.

  • How do you set value to this field @AnushuMishra Mar 20, 2019 at 9:56
  • @PrathapGunasekaran This will just add the field in the table. To add the content in the field Data Patch needs to be created. You can read about them at devdocs.magento.com/guides/v2.3/extension-dev-guide/… Mar 20, 2019 at 10:19
  • @AnshuMishra this is adding new field in the table, but not able to show the fields in the customer edit and admin edit pages. How to implement that ? Aug 6, 2019 at 4:27
  • @santhoshnsscoe you need to add customer attribute using data patch. Aug 6, 2019 at 4:45
  • @AnshuMishra yeah, added to used_in_forms using the patch. My attribute is textarea. I am getting this error in the admin. The "formElement" configuration parameter is required for the "custom" field. Aug 6, 2019 at 6:19

You can use InstallSchema Script to do that. Kindly refer below code:


namespace vendor\module\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;

class InstallSchema implements InstallSchemaInterface
    * {@inheritdoc}
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)

        $table = $setup->getConnection()->newTable(
            ['identity' => true, 'nullable' => true],
            'Referred By'
            )->setComment("Id of the customer that sent the Referral code");


  • ok, and then how can I add the field inside the entity model of customer? Because this field then I need to get and set Mar 20, 2019 at 9:33
  • 1
    @magefms M2.3 should be added via declarative schema even though it acepts, correct me if am wrong Mar 20, 2019 at 9:36
  • @MirkoRapisarda Try with customer attribute for your requirement you can't do this via custom field in a table. Mar 20, 2019 at 9:37
  • @PrathapGunasekaran, yeah you are right but just giving him an easier way for now since it also works for 2.3.
    – fmsthird
    Mar 20, 2019 at 9:40
  • 1
    @PrathapGunasekaran but with custom attribute, I lost the possibility to add a foreign key relationship, for this reason I want to add it as a real column Mar 20, 2019 at 9:40

Create new customer custom attribute for referral_code while installing referral module. here is a link to follow how to add custom attribute in customer section.


you can fetch custom attribute value from customer object using below code.


replace FIELD_NAME by your custom attribute name

  • but in this way I lost the possibility to add the referenced_by field as a foreign key Mar 20, 2019 at 9:37

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.