2
votes

On my own Magento 2 custom module, I want to install a custom database table. This is the InstallSchema class code:

<?php
namespace MyVendor\MyModule\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

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',
                    \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                    null,
                    ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                    'Greeting ID'
                )
                ->addColumn(
                    'message',
                    \Magento\Framework\DB\Ddl\Table::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.

  1. Attached an xdebug session with breakpoints inside the function, never called.

  2. Removed the module line in setup_module database table and re-run bin/magento setup:upgrade

  3. 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
Did you enable you module? bin/magento module:enable MyVendor_MyModule - wesleywmd
Delete the module entry from 'setup_module' table and remove your module form config.php file. again try to enable your module. magento 2 table will be created when the module installed. - Hariharan
The module is enabled. Tried deleting the entry on config.php, delete the setup_module record on the database, then setup:upgrade, nothing works. - wildchild
Also tried to create a new module, same thing. I guess there's something wrong with the InstallSchema code or maybe the folder structure of my module. The InstallSchema file is under MyVendor/MyModule/Setup folder. - wildchild

4 Answers

1
votes

@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.

<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="declarative_table">
        <column xsi:type="int" name="id_column" padding="10" unsigned="true" nullable="false" comment="Entity Id"/>
       <column xsi:type="int" name="severity" padding="10" unsigned="true" nullable="false" comment="Severity code"/>
        <column xsi:type="varchar" name="title" nullable="false" length="255" comment="Title"/>
       <column xsi:type="timestamp" name="time_occurred" padding="10" comment="Time of event"/>
       <constraint xsi:type="primary" referenceId="PRIMARY">
           <column name="id_column"/>
       </constraint>
   </table>
</schema>

Then run these command // ubuntu

php bin/magento setup:upgrade && php bin/magento cache:flush && php bin/magento cache:clean
1
votes

In my case the package name was wrong. After replacing by right package name issue is fixed.

namespace Package_Name\Module_Name\Setup;
0
votes

Please declare and add dependencies in InstallSchema file as given below:-

namespace <Your_Package>\<Your_Module>\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

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

0
votes

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.