On my own Magento 2 custom module, I want to install a custom database table. This is the InstallSchema class code:
<?php
namespace MyVendorMyModuleSetup;
use MagentoFrameworkSetupInstallSchemaInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupSchemaSetupInterface;
class InstallSchema implements InstallSchemaInterface
{
/**
* @inheritdoc
*/
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$table = $setup->getConnection()
->newTable($setup->getTable('my_table'))
->addColumn(
'greeting_id',
MagentoFrameworkDBDdlTable::TYPE_INTEGER,
null,
['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
'Greeting ID'
)
->addColumn(
'message',
MagentoFrameworkDBDdlTable::TYPE_TEXT,
255,
['nullable' => false, 'default' => ''],
'Message'
)->setComment("Greeting Message table");
$setup->getConnection()->createTable($table);
$setup->endSetup();
}
}
But the install method is not being executed.
-
Attached an xdebug session with breakpoints inside the function, never called.
-
Removed the module line in setup_module database table and re-run
bin/magento setup:upgrade
-
Set the developer mode, disable cache, run a
setup:di:compile
, still fails.
Any ideas?
I’ve also tried to use UpdateSchema changing the module version, no luck.
I’m running Magento 2 on a Ubuntu Server virtual box. Permissions on folders are set correctly.
4
Answers
Found the solution. It was the module name in the module.xml file. The setup:upgrade installer search for the InstallSchema in a case-sensitive folder path, so if you declare the module as
my_module
it will search on MyVendor/my/module/Setup folder.
I resolve it by replacing the name with:
MyModule
So the installer will search correctly on MyVendor/MyModule/Setup folder.
I found the problem with the "old-way", by placing log messages on the Install.php file on the Magento setup folder.
Please declare and add dependencies in InstallSchema file as given below:-
Then clear the cache and remove data from generated folder and remove module entry from setup_module.
Make sure that you have defined correct setup version in your module.xml file
@wildchild great to get it working with another solution. In your question, you did not mention if you are using 2.2.* or 2.3.*, you just stated Magento 2.
I had the same issue after moved from one server to another and upgrading from 2.2 to 2.3 my custom module stop working, the PHP script not creating the table in the database.
I later found out that Magento has changed the music now you have to use declarative schema structure
The
Module_Vendor/Module_Name/etc/db_schema.xml
file declares a module’s database structure.See the sample below and you should read more here
Create a table
The following example creates the declarative_table table with four columns. The id_column column is the primary key.
Then run these command
// ubuntu
In my case the
package name
was wrong. After replacing by right package name issue is fixed.