cancel
Showing results for 
Search instead for 
Did you mean: 

Install Schema Not Working

SOLVED

Install Schema Not Working

Hello,

We are trying to create custom tables when installing the module but it is not working. We have followed many guides but didn't get success. So can any one help us. We have created three files:

1. registration.php

2. etc/module.xml

3. Setup/InstallSchema.php

When we run upgrade, module is listed in config file but table is not created.

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Install Schema Not Working

I have using all your code and try to create table and module...

1. Please add setup_version in module.xml file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Test_Module" setup_version="0.0.1">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>

 2. Please add these two line in your InstallSchema.php code..

$installer = $setup;

$installer->startSetup();

 

<?php
namespace Test\Module\Setup;

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

class InstallSchema implements InstallSchemaInterface
{
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;

$installer->startSetup();

try {
$table = $setup->getConnection()
->newTable($setup->getTable('test_custom_table'))
->addColumn(
'test_id',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
null,
['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
'Test ID'
)
->addColumn(
'test_content',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
255,
['nullable' => false, 'default' => ''],
'Test Content'
)->setComment("Test Custom table");
$setup->getConnection()->createTable($table);
} catch(Exception $err) {
\Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info($err->getMessage());
}
}
}

 Once the extension is created, then we are creating installSchema.php file after executing the extension. Therefore, we have deleted the module form setup_module table and again run the command ....

php bin/magento setup:upgrade

php bin/magetno setup:static-content:deploy -f

php bin/magento cache:flush

View solution in original post

10 REPLIES 10

Re: Install Schema Not Working

Hi @supportopeadb2,

 

Maybe you want to check Declarative Schema: https://devdocs.magento.com/guides/v2.4/extension-dev-guide/declarative-schema/

Which version of Magento are you using?

Re: Install Schema Not Working

Hello,

We are using magento 2.3 and 2.4.

Thanks!

Re: Install Schema Not Working

@supportopeadb2 is it possible to share the code you are using in those files? Is your module lised into the setup_module table?

Re: Install Schema Not Working

The installer won't run again if it was already executed.
Removing the record from setup_module with module = 'MyVendor _Helpdesk' should make it run again.

Re: Install Schema Not Working

Hello,

Module entry is not present in setup_module table. Below are files we have created:

1. registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Test_CustomTable',
    __DIR__
);

2. etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Test_CustomTable">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

3. Setup/InstallSchema.php

<?php
namespace Test\CustomTable\Setup;

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

class InstallSchema implements InstallSchemaInterface
{
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
      $table = $setup->getConnection()
          ->newTable($setup->getTable('test_custom_table'))
          ->addColumn(
            'test_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            null,
            ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
            'Test ID'
          )
          ->addColumn(
            'test_content',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            ['nullable' => false, 'default' => ''],
            'Test Content'
          )->setComment("Test Custom table");
          $setup->getConnection()->createTable($table);
      }
}

Please check and let us know where is the issue.

Thanks!

Re: Install Schema Not Working

Hello,

We have already followed your mentioned guide and many other but didn't get success. So please check our code and let us know where is the issue.

Thanks!

Re: Install Schema Not Working

Please verify that you have followed a proper directory structure and also share the full path of every related file.

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

Re: Install Schema Not Working

Change the code of etc/module.xml file.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Test_CustomTable" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>
Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

Re: Install Schema Not Working

Referring to your code shared in the comment I found want thing were missing.

 

You forgot to added the setup_version in your module.xml file

Change this from

<module name="Test_CustomTable">

To

<module name="Test_CustomTable" setup_version="1.0.0">