Magento 2 - InstallData.php does not run again after first time
-
07-01-2021 - |
题
I have created my custom module to add more attributes for customers.
The InstallData.php
worked perfectly for the first time and added all the attributes I wanted.
However, I need to add more attributes. I updated my InstallData.php
and run php bin/magento setup:upgrade
, but the new attributes are not added.
What did I miss in this problem?
解决方案
Delete your module from setup_module
table and run php bin/magento setup:upgrade
其他提示
Delete your module from setup_module
table in a database and also require to delete your database table which is useful in InstallData.php
.
If you don't want to delete your database table then you can also use UpgradeSchema.php
file to add more attributes in the database.
Create Vendor/Module/Setup/UpgradeSchema.php
<?php
namespace Vendor\Module\Setup;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UpgradeSchemaInterface;
class UpgradeSchema implements UpgradeSchemaInterface
{
public function upgrade(
SchemaSetupInterface $setup,
ModuleContextInterface $context
) {
$installer = $setup;
$installer->startSetup();
$installer->getConnection()->addColumn(
$installer->getTable('table_name'),
'table_field_name',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => '2M',
'nullable' => false,
'default' => null,
'comment' => 'Custom Attribute',
]
);
$installer->endSetup();
}
}
I hope this will help you.
The right way of doing it is using UpgradeData. This way you don't touch on the already existing data installed by InstallData. Only thing to note is change the setup_version
in module.xml.
There is an answer for this question already here