Skip to main content
16 votes

Magento 2 How to add foreign key in update schema

Try this: ->addForeignKey( $installer->getFkName('<ChildTable>', 'entity_id', '<ParentTable>', 'entity_id'), 'entity_id', $installer->getTable('<ParentTable&...
Ronak Chauhan's user avatar
8 votes

How to fix this "Running schema recurring.Unique constraint violation found"

To see the actual error, you need to run the data upgrade command on it's own: php bin/magento setup:db-data:upgrade And suddenly, after many hours of searching and debugging one gets to see the ...
Alex's user avatar
  • 185
6 votes
Accepted

Make column unique in install schema script

You can use addIndex method having type \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE, like: $table = $installer->getConnection()->newTable( $installer->getTable(...
Siarhey Uchukhlebau's user avatar
6 votes
Accepted

Magento 2: Run a uninstall script when module is uninstalled via command line?

Magento Setup Uninstall script The below page in the Magento DevDocs explains the process of module uninstallation. It explains the steps that are taken and one of those steps is running Uninstall ...
7ochem's user avatar
  • 7,572
6 votes

Recurring Upgrade Scripts in Magento 2.0, 2.1, or 2.2?

Alan, first of all, I want to say thank you for your blog. I learned a lot of Magento thing using it. Thanks a lot. About the Recurring.php: These scripts are fired every time u run setup:upgrade ...
Stepan Furman's user avatar
6 votes
Accepted

Force-run module install script in Magento 2

If you are trying to run installation scripts for a "local" module (not installed with composer) and the module is already installed, you should: Remove module installation registry from database, in ...
Dani Garval's user avatar
6 votes
Accepted

Magento 2 how to add data to custom column in sales_order table

Setup/InstallSchema.php <?php namespace [Vendor]\[Module]\Setup; use Magento\Framework\Setup\InstallSchemaInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\...
Sweety Masmiya's user avatar
5 votes
Accepted

Custom customer attribute are throwing error at Customer edit Page - Magento2

We should try with getCustomAttribute() $block->getCustomer()->getCustomAttribute('codes_for_apply');
Khoa TruongDinh's user avatar
5 votes
Accepted

How to create MySQL Triggers through setup script

We can use \Magento\Framework\DB\Ddl\TriggerFactory to create mysql triggers through setup script: (not test yet) class UpgradeSchema implements UpgradeSchemaInterface { /** * @var \Magento\...
Khoa TruongDinh's user avatar
5 votes
Accepted

Magento2 What's the point of having both InstallSchema and UpgradeSchema

I got this question in a training I was giving, and we tried out if we need InstallSchema / InstallData at all. It turned out we don't need it, as the Upgrade classes are executed on installation too. ...
Andreas von Studnitz's user avatar
4 votes
Accepted

Add category attribute to custom attribute group

Thanks to @SohelRana I found out that you now have to write a bunch of additional XML to make anything appear in the category form. Since the answer linked above does not explain how to add a new ...
Fabian Schmengler's user avatar
4 votes

Magento 2 How to add foreign key in update schema

Sample for foreign key, <table comment="projectfinder_attributevalue Table" engine="innodb" name="projectfinder_attributevalue" resource="default"> <column comment="Entity Id" identity="...
Kazim Noorani's user avatar
4 votes
Accepted

Magento 2 - InstallData.php does not run again after first time

Delete your module from setup_module table and run php bin/magento setup:upgrade
Vinu Sebastian's user avatar
4 votes
Accepted

how to add collation( utf8_general_ci) using install schema in magento2?

Add this below script in InstallSchema.php file : <?php namespace VendorName\ModuleName\Setup; use Magento\Framework\Setup\InstallSchemaInterface; use Magento\Framework\Setup\...
Rohan Hapani's user avatar
  • 17.5k
4 votes
Accepted

Magento 2 : create view table via setup script

Magento itself in the rather newish inventory project creates the inventory_stock_* tables itself via plain SQL: $sql = "CREATE SQL SECURITY INVOKER VIEW {$viewToLegacyIndex} ...
Simon's user avatar
  • 5,746
3 votes
Accepted

I need to add to tables to database using InstallSchema.php How can i do that?

You can add as many table as you want in your installSchema file. Refer below code <?php namespace Test2\Helloworld\Setup; use \Magento\Framework\Setup\InstallSchemaInterface; use \Magento\...
Priyank's user avatar
  • 7,712
3 votes
Accepted

Customized field got disappeared in catalog_product_flat_1 table

The flat tables for products and categories are dynamic. They are generated on the fly when you reindex based on several criterias. For the products it's based on whether or not the product attribute ...
Raphael at Digital Pianism's user avatar
3 votes
Accepted

How can i can run install script from controller without error

The constructor method for Mage_Core_Model_Resource_Setup defines a single parameter which is required to create an object instance: public function __construct($resourceName) {...} The $...
duttyman's user avatar
3 votes

Install script will not execute

You can delete the record "pinterestimage_setup" from the database table core_resource instead of changing "pinterestimage_setup" to "mymod_pinterestimage_setup"
user51486's user avatar
3 votes
Accepted

How can I create an attribute as "not configurable" from an install script?

You should actually use Mage_Catalog_Model_Resource_Setup::addAttribute() instead of Mage_Eav_Model_Entity_Setup::addAttribute() As you have found out in the code, Mage_Eav_Model_Entity_Setup::...
Sébastien's user avatar
3 votes

Magento 2 datatypes. Need TIME (of day)

I solved this by using DATETIME initially $table = $installer->getConnection() ->newTable($installer->getTable('MYTABLE')) ->addColumn( 'MYCOLUMN', ...
Kampaviineri's user avatar
3 votes
Accepted

How do I specify unique constraint for multiple columns in InstallSchema.php?

Problem solved: in the script, I directly executed the request: $myRequest = "ALTER TABLE..."; $installer->getConnection()->query($myRequest ); EDIT: finally found how to do it with the ...
enrico69's user avatar
  • 515
3 votes

Magento 2 How to add foreign key in update schema

1 more important thing you need to do is make your column in foreign table is THE SAME as the column in your primary table or it will throw an error when you try to run "php bin/magento setup:upgrade",...
fudu's user avatar
  • 1,298
3 votes
Accepted

Magento 2 : Create product attribute with select box install script

Please do following suggestions in the install script. Make 'user_defined' => true from 'user_defined' => false In case the above solution didn't work, then remove the backend and source ...
Amit Bera's user avatar
  • 77.6k
3 votes
Accepted

Magento 2 : Creating widget using install script

This worked for me please try it. $megaMenuWidget = [ 'instance_type' => 'Magento\Cms\Block\Widget\Block', 'theme_id' => 2, 'title' => 'Test Megamenu Horizontal', 'store_ids' ...
Vindhuja's user avatar
  • 1,395
3 votes

Recurring Upgrade Scripts in Magento 2.0, 2.1, or 2.2?

I think in devdocs there is another place where this is explained. Magento executes your module’s recurring schema event class after every schema installation or upgrade stage. This class makes final ...
Bartosz Kubicki's user avatar
3 votes

Magento 2 - Create product attribute programmatically

Use below code in: Vendor/Module/Setup/InstallData.php or Vendor/Module/Setup/UpgradeData.php $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); $eavSetup->...
Sairam Sigirisetty's user avatar
3 votes
Accepted

How to fix this "Running schema recurring.Unique constraint violation found"

"Running schema recurring.Unique constraint violation found" this occurs if there is duplicate entry in any table of project db and high probability to occur this problem is when you export db and ...
kaustubh borate's user avatar
2 votes

my table is not installing in Magento 2

Some time there may an entry for you module in app/etc/config.php with value 0. And if you run the command bin/magento setup:upgrade then it will not upgrade your plugin. Change the vaue to 1 ...
Manish's user avatar
  • 3,114
2 votes
Accepted

Magento2 installSchema

If you want to update existing table/schema adding/updating row then, Step 1: Create UpgradeSchema.php under Setup folder. Get Idea from following code. namespace Vendor\ModuleName\Setup; use ...
Rushvi's user avatar
  • 2,843

Only top scored, non community-wiki answers of a minimum length are eligible