0

I have created a module that has a setup that creates a table for me. However, the table does not exist after installing the module. I don't know why.

Error:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mag2new.formmodule_users' doesn't exist, query was: DESCRIBE formmodule_users

File is: Setup/InstallSchema.php

namespace Company\FormModule\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Adapter\AdapterInterface;

class InstallSchema implements InstallSchemaInterface
{
    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();
        $table = $installer->getConnection()->newTable(
            $installer->getTable('formmodule_users')
        )->addColumn(
            'id',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            null,
            ['identity' => true, 'nullable' => false, 'primary' => true],
            'ID'
        )->addColumn(
            'name',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            ['nullable' => true],
            'Firstname'
        )->addColumn(
            'lastname',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            ['nullable' => true],
            'Lastname'
        )->addColumn(
            'address',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            ['nullable' => true],
            'Address'    
        )->setComment(
            'FormModule User Table'
        );
        $installer->getConnection()->createTable($table);
        $installer->endSetup();
    }
}

I've tried setup:upgrade, setup:db-schema:upgrade, setup:db-data:upgrade, cache:clean and deleting the record in setup_module. Any tips?

I used this for my module: How do I make a module with the form component?

1

try this code checking version

namespace Company\FormModule\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Adapter\AdapterInterface;

class InstallSchema implements InstallSchemaInterface
{
    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();
        if (version_compare($context->getVersion(), '2.0.1') < 0){
            $table = $installer->getConnection()->newTable(
                $installer->getTable('formmodule_users')
            )->addColumn(
                'id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                null,
                ['identity' => true, 'nullable' => false, 'primary' => true],
                'ID'
            )->addColumn(
                'name',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                ['nullable' => true],
                'Firstname'
            )->addColumn(
                'lastname',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                ['nullable' => true],
                'Lastname'
            )->addColumn(
                'address',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                ['nullable' => true],
                'Address'    
            )->setComment(
                'FormModule User Table'
            );
            $installer->getConnection()->createTable($table);
        }
        $installer->endSetup();
    }
}

compare your current module version. for example module.xml

<module name="Company_FormModule" setup_version="2.0.0"></module>

so your current version 2.0.0 you update your version edit setup_version = "2.0.1" and InstallSchema.php

if (version_compare($context->getVersion(), '2.0.1') < 0){
   // your table
}

then finally setup:upgrade and cache:clean

|improve this answer|||||
0

Give this code a try , it worked for me ( replace this code in your public function ):

$table = $setup->getConnection()
    ->newTable($setup->getTable('formmodule_users'))
    ->addColumn(
        'id',
        \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
        null,
        ['identity' => true, 'nullable' => false, 'primary' => true],
        'ID'
    )->addColumn(
        'name',
        \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
        255,
        ['nullable' => true],
        'Firstname'
    )->addColumn(
        'lastname',
        \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
        255,
        ['nullable' => true],
        'Lastname'
    )->addColumn(
        'address',
        \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
        255,
        ['nullable' => true],
        'Address'    
    )->setComment("FormModule User Table");
$setup->getConnection()->createTable($table);
$setup->endSetup();
|improve this answer|||||
  • Hmm.. this did not work for me. I replaced the code, upgraded, cleaned cache and removed the record in setup_module. Did my code work for you or not? – Oscar Apr 3 '18 at 13:01
  • okay, when you installed your module first time did you add this schema install or first you added your module and then added install schema ? – Naveed Asim Apr 3 '18 at 13:04
  • I'm not sure what you mean. I ran setup:upgrade after all the files were added to my module. – Oscar Apr 3 '18 at 13:07
  • my mean, when you installed the module was schema part of that module or you added schema later ? disable your module, ( also delete entry from your app/etc/config.php and delete your module entry from setup_module table. And then install your module – Naveed Asim Apr 3 '18 at 13:11
  • The schema is a part of the module, I added the link to the guide in my question I was following if you'd like to review the code. I deleted the entry in config.php and in setup_module, but still the same error. – Oscar Apr 3 '18 at 13:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.